Search code examples
phparrayskey

The difference between unquoted, single-quoted, and double-quoted array keys


I know that it's more performant to use single-quoting versus double-quoting when accessing/processing string values, but I was wondering if there's any performance benefits in omitting the quotations entirely.

$a = array('table' => 'myTable');

$table = $a['table'];

versus

$a = array(table => 'myTable');

$table = $a[table];

Solution

  • Yes. In your second example the PHP processor checks if "table" is defined as a constant before defaulting it back to an array key, so it is making one more check than it needs to. This can also create problems.

    Consider this:

    const table = 'text';
    
    $a = array( table => 'myTable', order => 'myOrder' );
    
    $table = $a[table]
    

    Now PHP interprets your array as $a[text] which is not what you want.