954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

$variable inside quotes

Which of the following is the proper syntax for the latest version of PHP?

echo "My name is $name. How are you?";
echo "My name is " . $name . ". How are you?";
cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

I'm not 100% Sure that one form is "more proper" than the other. I do realize that one form is more readable.... but I don't know if one is better syntax than the other?

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

In Fact,

According to http://www.php.net , ( http://us2.php.net/manual/en/function.echo.php )
you'll see that it shows both examples. It seems that it's a matter of whatever the programmer feels more comfortable with. Let me know if you find any further information on it though please.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

they both work just fine, HOWEVER if you are referencing an array with a named term such as the following example neither one of these two will work:

echo "my name is $person['f_name'] and my last name is $person['l_name'] ";
echo "my name is $person["f_name"] and my last name is $person["l_name"] ";

so you would be forced to explicitly state that you are citing a variable as in
echo "my name is ".$person['f_name']." and my last name is ".$person['l_name'];

paradox814
Posting Whiz
351 posts since Oct 2004
Reputation Points: 13
Solved Threads: 4
 

Often, arrays will work without any quotes at all ... $myarray[foo]. This is probably a long shot, but can you use double quotes for an array row by escaping them? i.e. $myarray[\"foo\"] ?? I doubt that ;)

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

I think the use of quotes and escaping the variable within echos is for the sake of exclusivity. In the case of variables containing escaping characters or handling secure connections, you can have problems. Although for the majority of tasks (as the examples show) exclusivity isnt really required.
I guess it depends on what you are doing with the arrays and the strings within them really. each to its own task to be honest.
If you want to dig a hole....

ReDuX
Junior Poster
127 posts since Sep 2004
Reputation Points: 12
Solved Threads: 5
 

I usually surround variables in "'s with { and }, thus:

[php]echo "This is a {$variable}."[/php]

This helps it work out array indeces and stuff, and tends to make it easier to read. As for which format, it usually depends on the context, I use both. Constants don't appear to be translated when in "'s, so you need the latter format in this case.

knight42
Newbie Poster
6 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You