| | |
$variable inside quotes
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Which of the following is the proper syntax for the latest version of PHP?
PHP Syntax (Toggle Plain Text)
echo "My name is $name. How are you?";
PHP Syntax (Toggle Plain Text)
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
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.
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.
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'];
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'];
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
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 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
http://www.vivaci.net - Quality Webhosting
http://gaming.vivaci.net - FAST UK Gaming servers
http://www.getsigned.org - Free Image Uploads
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.
[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
Dreamweaver | PHP | MySQL | Cinema 4D | Chocolate
![]() |
Similar Threads
- Get a String That is Inside Quotes (C++)
- Increment Variable Name Inside Loop (Java)
- JS variable inside HTML (JavaScript / DHTML / AJAX)
- Trying to change a variable from inside a Class (Python)
Other Threads in the PHP Forum
- Previous Thread: Validating Email Addresses - A little extra help!
- Next Thread: Newbie needs help with DB query
Views: 5705 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access address ajax apache api array autoincrement beginner binary broken cakephp checkbox class cms code cron curl database date dehasher directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu method methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure support! syntax system table tutorial update updates upload url validation validator variable video web xml youtube






