...

View Full Version : use of quotes in array references



black3842
04-20-2004, 08:42 AM
I've noticed that all of the following work
$_SERVER["PHP_SELF"]
$_SERVER['PHP_SELF']
$_SERVER[PHP_SELF]

I've been using single quotes, but what is the convention? :confused:

missing-score
04-20-2004, 08: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.

raf
04-20-2004, 08:53 AM
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()

missing-score
04-20-2004, 08:58 AM
Same here :), SQL gets annoying if you have to escape all the single quotes.

brothercake
04-20-2004, 06: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");

etc.

raf
04-20-2004, 06:17 PM
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.

missing-score
04-20-2004, 06:53 PM
I would do the same as raf, I find it annoying trying to find variables in double quotes.

black3842
04-20-2004, 10: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.

firepages
04-21-2004, 01: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';
DEFINE('glug','whataaaaa');
$_SERVER['glug'] = 'blaaargh';
DEFINE('PHP_SELF','glug');
?>
without running it , what will the following print ?
<?
echo $_SERVER[PHP_SELF];
echo $_SERVER['PHP_SELF'];
?>


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum