View Full Version : use of quotes in array references
04-20-2004, 09:42 AM
I've noticed that all of the following work
I've been using single quotes, but what is the convention? :confused:
04-20-2004, 09:47 AM
All of those should work, however I really dont reccomend the last one (no quotes)
Generally, you will want to use the second one (single quotes), as for strings, single quotes are very slightly faster. On this small scale it wont make much (if any) difference, but its a good habbit to get into using double quotes only where needed.
If the script is parsed, then values between single quotes are taken literally, so they are not parsed. This is the slight gain in processingtime.
All text between double quotes is parsed.
so if you have
$test = 'bla';
echo ('$test'); // prints $test
echo ("$test"); // prints bla
But there are hardly situations where you need double quotes. I only use it for sql-statements and sometimes code that i eval()
04-20-2004, 09:58 AM
Same here :), SQL gets annoying if you have to escape all the single quotes.
04-20-2004, 07:06 PM
That's an advantage though isn't it? It means you can go:
echo("Hello my name is $name and I live in $town");
Well, i'd personally use
echo('Hello my name is ' . $name . ' and I live in ' . $town);
Because i find it easier to read + with colourcoding, i can immedeately see what are strings and what are variables.
Don't think there is a noticable perfomancediffernce, but for me it results in fewer errors.
04-20-2004, 07:53 PM
I would do the same as raf, I find it annoying trying to find variables in double quotes.
04-20-2004, 11:02 PM
Thanks guys, yeah I do it the same as raf normally, for readability, just wasn't sure if arrays were handled differently....
Thanks for the feedback.
04-21-2004, 02:31 AM
.. just one other thing which did not get mentioned, with no quotes at all you have possibly a whole new variable ...
$glug = 'grrrrrrr';
$_SERVER['glug'] = 'blaaargh';
without running it , what will the following print ?
in other words , given an unquoted argument to an array PHP looks first for a constant (http://www.php.net/constants) of that name , then if it fails it adds the quotes itself (thowing a warning) and looks for the quoted array variable.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.