$array = array(
'first element',
'second element',
'third element',
);
echo $array['1'];
echo $array[1];
They both produce the same result.
I am asking this question because I noticed for example drupal accesses the index as string literals, where as php.net uses integers.
Are there advantages of one over the other?
Of course I know why $array[foo] is bad.
It’s indifferent in terms of semantics.
When you do
$array['string']and'string'is a number in base 10 without leading zeros and decimal point that fits on the integer range (i.e., is between-PHP_INT_MAX-1andPHP_INT_MAX), it will be converted into an integer.In terms of performance, it’s preferrable to use an integer directly because that will skip the conversion step.