$variable inside quotes

Reply

Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

$variable inside quotes

 
0
  #1
Jan 17th, 2005
Which of the following is the proper syntax for the latest version of PHP?

  1. echo "My name is $name. How are you?";
  1. echo "My name is " . $name . ". How are you?";
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: $variable inside quotes

 
0
  #2
Jan 17th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: $variable inside quotes

 
0
  #3
Jan 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: $variable inside quotes

 
0
  #4
Jan 17th, 2005
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'];
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 130
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: $variable inside quotes

 
0
  #5
Jan 17th, 2005
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
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 127
Reputation: ReDuX is an unknown quantity at this point 
Solved Threads: 5
ReDuX's Avatar
ReDuX ReDuX is offline Offline
Junior Poster

Re: $variable inside quotes

 
0
  #6
Jan 17th, 2005
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....
I'm pink, therefore, im spam.
http://www.vivaci.net - Quality Webhosting
http://gaming.vivaci.net - FAST UK Gaming servers
http://www.getsigned.org - Free Image Uploads
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 6
Reputation: knight42 is an unknown quantity at this point 
Solved Threads: 0
knight42's Avatar
knight42 knight42 is offline Offline
Newbie Poster

Re: $variable inside quotes

 
0
  #7
Jan 17th, 2005
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.
www.knight42.org.uk
Dreamweaver | PHP | MySQL | Cinema 4D | Chocolate
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5705 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC