Reputation: 63465
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
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
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
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