Posted on

Accessing associative PHP array without quotes

Basically the question is if $color[standard] is better than $color['standard'] ? Basically the first one is working as well, AS LONG AS there is no constant called “standard” – which is usually not the case.

Still this is bad coding style and quotes should always be used.

$color = array(
    'success' => '#00ff00', 
    'warning' => '#ffff00', 
    'error' => '#ff0000');
$standard = 'warning';

// working if no constant "standard" but BAD style
echo $color[standard]; 

// working and GOOD coding style
echo $color['standard'];