opensas
opensas

Reputation: 63465

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];

Upvotes: 3

Views: 484

Answers (3)

grantwparks
grantwparks

Reputation: 1153

The difference, according to research, between "string" and 'string' is so negligible as to be non-existent. Not that it matters much, BUT it's faster to do

echo "this is {$var1} and {$var2} and {$var3}";

than it is to

echo 'this is ' . $var1 . ' and ' . $var2 . ' and ' . $var3;

Upvotes: 1

Paige Ruten
Paige Ruten

Reputation: 176675

You should always quote your strings. In your second example, PHP does convert table and order to strings, but if table or order were defined as constants, PHP would use the value of the constant instead of the string 'table' or 'order'.

Upvotes: 5

carbin
carbin

Reputation: 3027

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.

Upvotes: 8

Related Questions