This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Jo
over 10 years

I wanna ask the differences

In this exercise, I can finish it with:

echo "<p>Hello! My name is " . $name . " and I am " . $age . " years old.";

however, I can also finish it with this:

echo "Hello! My name is $name and I am $age years old.";

Does the second one right? Or what’s the difference between them?

Answer 527d033e80ff33d04d003aa7

1 vote

Permalink

the first one is correct. remember php is a server side language unlike html. so if you run

“Hello! My name is $name and I am $age years old.”

in html that is how it will appear on the page. it will not access the php variables you may have created.

so in your first example you are using php to insert the variables into your html. the “.” is a concatenation string operator. you can append your variable to a string.

basically your first example is a far better practice to get into . the 2nd example may run in the editor but in a live environment it will cause you problems.

points
Submitted by daireof
over 10 years

2 comments

Jo over 10 years

OK, got it, I will try the two in a server, to find the difference. Thank you!

daireof over 10 years

go to here; http://writecodeonline.com/php/ insert your 2nd line into the editpr and see the result

Answer 5278bca0f10c606b7f00c758

0 votes

Permalink

You can do both ways since both are correct but the first part would be preferrable when it comes to formatting your output. May I ask which part specifically are you referring to? Are you referring to the dots or the p tag?

points
Submitted by macky
over 10 years

1 comments

Jo over 10 years

Yes, I’m curious about the dots and

. I had thought I must use them to arrange the string.