Web Hosting Talk







View Full Version : What is the purpose of the single quote in key part of an array


Syphic
08-20-2008, 04:16 PM
I was having a discussion with some co-workers today and thought I might ask others options.

What is the purpose of the single ticks in an array?

Example:

$arr['key'] = 'value';

The part we are referring to is the ['key'] instead of doing [key]?

Anyone has a good reason? I just could not remember sense it has become a happy to put the [' in there.

shockuk
08-20-2008, 04:30 PM
I think it's so PHP knows whether you're referring to the array by name (with a string), or by position number (by integer). Each array type has a different name but I forgot what they are.

I.E.
$arr['3']
-- is not the same as
$arr[3]

Burhan
08-20-2008, 05:56 PM
key -- without the quotes, is a constant.
'key' -- with the quotes, is a string.

If you develop with error reporting enabled and the error level to be E_ALL, then you'll see the problem (highlighted as a notice):


error_reporting(E_ALL);
$x['key'] = 'value';
echo $x['key'];
echo $x[key];



Notice: Use of undefined constant key - assumed 'key'

ak7861
08-20-2008, 06:24 PM
JFYI, if you do a memory check on both... the one with single quotes takes less memory.

Syphic
08-20-2008, 11:13 PM
Thank you from everyone