943,524 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 9732
  • PHP RSS
Jan 17th, 2005
0

$variable inside quotes

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

PHP Syntax (Toggle Plain Text)
  1. echo "My name is $name. How are you?";
PHP Syntax (Toggle Plain Text)
  1. echo "My name is " . $name . ". How are you?";
Similar Threads
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jan 17th, 2005
0

Re: $variable inside quotes

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?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 17th, 2005
0

Re: $variable inside quotes

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 17th, 2005
0

Re: $variable inside quotes

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'];
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Jan 17th, 2005
0

Re: $variable inside quotes

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
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jan 17th, 2005
0

Re: $variable inside quotes

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....
Reputation Points: 12
Solved Threads: 5
Junior Poster
ReDuX is offline Offline
127 posts
since Sep 2004
Jan 17th, 2005
0

Re: $variable inside quotes

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knight42 is offline Offline
6 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Validating Email Addresses - A little extra help!
Next Thread in PHP Forum Timeline: Newbie needs help with DB query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC