| | |
$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
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess 5.2.10 access action ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email error file files folder form forms function functions google header href htaccess html image include insert integration ip java javascript joomla limit link login loop mail menu mlm mod_rewrite multiple mysql mysqlquery neutrality oop parse paypal pdf php problem query radio random record recursion regex remote script search security server sessions sms soap sockets source space sql structure syntax system table thesishelp tutorial update upload url validation validator variable video web window.onbeforeunload=closeme; xml youtube






