Reason to use " . name . "?


#1

Any reasons to use
"Hello, my name is " . name . "

over

"Hello my name is $name

?


#2

. is used to concatenate strings...

If you want to pass a variable you will have to use . to add it to the string and print it out.

For example:

print "Hi, my name is " . $name . "and I like soccer";

or even

print "Hello there this is part 1" . " and this is part 2";

If you don't use it and try to print a string with multiple entries you'll get a parse error.


#3

@conolyb1 not necessarily true. this code:

echo "this is $num1 and $string1 and $string2 and $num2.";

will print out the variable values within the string just fine (assuming they've been assigned).


#4

You're awfully late with this reply. The thread is moribund and we really shouldn't be necrotizing old topics as their subscribers have long since departed this scene.

What you fail to point out here is that when using single quotes, our only option is concatenation when iterpolating strings. The double quote method permits embedded variables (literal interpolation).

Closing this topic in two hours.


#5

Very true about the single quotes vs. double quotes. I didn't point that out because single quotes weren't mentioned at all in the question. The only reason I posted was so that others who came across this thread would see that you could do that without getting a parse error.


#6

However, we need not let the question constrain a full explanation. Between us, we succeeded.

Still going to close this topic. Ping me if you have more to add, and I'll re-open it.


#7