Numeric and integer keys

<?php
$a
= 1;
$b ->$a = 'foo';
var_export((array)$b);
var_export(array(1 => 'foo'));
?>

Results:
array (
  '1' => 'foo',
)array (
  1 => 'foo',
)

Observe the '1'. The array element after casting can not be reached via the [] operator. (And no, "'1'" wont do the trick.)

Comments

It's casting the 1 to a

It's casting the 1 to a string because class variables cannot start with numbers, just like functions. Doing $b->$a caused $a to be converted to a string. However, that is very WTF about not being able to retrieve the array element using []. At least you can still get it if $b is still an object and you can do var_export($b->$a).

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options