views:

133

answers:

3

I know that it's more performant to use '' delimited strings rather than ""...

but I was wondering if there's any performance improvemente doing this


$a = array( 'table' => 'myTable', 'order' => 'myOrder' );

$table = $a['table']

instead of


$a = array( table => 'myTable', order => 'myOrder' );

$table = $a[table]

I guess so, but just wanted to know your opinion...

+7  A: 

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.

Norse
+4  A: 

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'.

yjerem
A: 

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