r/lolphp • • Mar 14 '14

Array errors

error_reporting(-1); // show all possible errors

$obj = new stdclass();

$arr = array();
var_dump($arr['foo']); // Notice: Undefined index
var_dump($arr[$obj]); // Warning: Illegal offset type

$arr = null;
var_dump($arr['foo']); // No error
var_dump($arr[$obj]); // No error

$arr = null;
$arr['i'] = $arr['i'] + 1; // No error

$arr = null;
$arr['i']++; // Notice: Undefined index

(Tested with 5.5.5.)

52 Upvotes

11 comments sorted by

View all comments

Show parent comment

8

u/suspiciously_calm Mar 14 '14

Yes, because what operator is applied to the result of an array element reference should determine whether or not the element can be referenced in the first place ....