Hi all,
I am conflicted as per if what I experience in this code should be
throwing a notice in E_STRICT:
<?php
error_reporting(E_ALL);
$foo = ['bar' => ['baz' => 5]];
$bar = ['box' => 'baz'];
// this will Notice: Undefined index: boom
var_dump(isset($foo['bar'][$bar['boom']]));
Essentially, my expectation is that anything inside an isset() would be
immune from notices of undefined indexes, but that is not the case with
nested array key lookups.
Is this a bad expectation to have? Or should isset() silence the nested
undefined index as well? I also realized this has always been the
behavior leading me to believe I might have a bad expectation here. In
any case wanted to check.
Thanks,
-ralph
On Sun, Jul 12, 2015 at 2:52 AM Ralph Schindler ralph@ralphschindler.com
wrote:
Hi all,
I am conflicted as per if what I experience in this code should be
throwing a notice in E_STRICT:<?php
error_reporting(E_ALL);
$foo = ['bar' => ['baz' => 5]];
$bar = ['box' => 'baz'];// this will Notice: Undefined index: boom
var_dump(isset($foo['bar'][$bar['boom']]));Essentially, my expectation is that anything inside an isset() would be
immune from notices of undefined indexes, but that is not the case with
nested array key lookups.
You're not getting a notice for the undefined index $foo['bar'][null]
,
that's basically what isset() is made to do. If you want to guard against
undefined indices in an array that you're using to dereference $foo
you
need this:
var_dump(isset($bar['boom'], $foo['bar'][$bar['boom']]));
Is this a bad expectation to have? Or should isset() silence the nested
undefined index as well? I also realized this has always been the
behavior leading me to believe I might have a bad expectation here. In
any case wanted to check.Thanks,
-ralph
Hi!
Is this a bad expectation to have? Or should isset() silence the nested
undefined index as well? I also realized this has always been the
That would be really hard to do IMO. Imagine $bar being an object and
implementing array access - so it needs somehow know that when offsetGet
is called, it's in isset expression context somewhere upstream?
While it may be nice in theory, in practice I'm not sure it's doable -
or worth making doable.
Stas Malyshev
smalyshev@gmail.com
Stas + Tjerk,
is called, it's in isset expression context somewhere upstream?
While it may be nice in theory, in practice I'm not sure it's doable -
or worth making doable.
Thanks for your input, I suspected there was a certain complexity I was
not immediately seeing.
-ralph