PHP PHP Basics Daily Exercise Program String Manipulation

Why is This Wrong?

The Output is correct and i did the logic correct maybe I didn't do it in the way the computer Wanted I set the Variable to "Rasmus" Which is what it wanted but it said it was wrong my code isn't incorrect from what I can Tell.

<?php

$firstName = "Rasmus";

$lastName = " Lerdorf";

$fullName = ($firstName .= $lastName);

echo($fullName);
?>

How do you post the image i don't see where i can

KRIS NIKOLAISEN
KRIS NIKOLAISEN
Pro Student 51,734 Points

Images are hard to work with. It is better to copy and paste your code and format it using markdown (see the cheatsheet at the bottom of this page)

Ok figured it out

I figured out how to post the code not the question itself just to specify

3 Answers

Steven Parker
Steven Parker
177,495 Points

I see three issues that would affect the challenge but would not be seen by looking only at $fullName:

  • there is a stray space at the beginning of the string " Lerdorf"
  • the ".=" operator modifies $firstName, but the challenge is expecting it to remain as it was ("Rasmus")
  • you'll need to add a space between the combined names without changing the individual names

Yeah You are Right I changed it to

$fullName = ("$firstName $lastName\n");

worked just fine that way

Steven Parker
Steven Parker
177,495 Points

Good job! :+1: I don't think the newline ("\n") was part of the instructions but it was nice that it allowed it. But in general you'll get best results with the challenges if you do only what the instructions ask for. "Extras" can confuse the validation!

Happy coding!

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,734 Points

The bummer message isn't very helpful. But one reason the challenge will fail is you have a space before Lerdorf in your lastName variable. Instead of a space there you can add one with concatenation to fullName as follows:

$fullName = $firstName . ' ' . $lastName;

In php whitespace doesn't matter so the space before that word is not the issue I am Having the issue is that it is saying that my variable $firstName is not equal to "Rasmus" which it clearly is.

Steven Parker
Steven Parker
177,495 Points

The ".=" alters $firstName, try echoing it out instead of (or along with) $lastName and see for yourself.

And whitespace might not matter in code, but it does matter inside strings.

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,734 Points

Ignore the error message about the firstName. That is why I said the bummer message isn't very helpful.

Steven Parker
Steven Parker
177,495 Points

The fact that $firstName gets changed and is no longer equal to "Rasmus" is quite pertinent!