> Excellent thinking.  One hitch... that goes fatal in 5.3 if $foo is a string.
> Here's how to write the test so it works the same way under 5.3 and 5.4:

Correct. However, it's worth noting that it only goes fatal if $foo is a string.

So as long as you know that the root variable is an array, it will
work fine.  So perhaps:

is_array($foo) && isset($foo['bar'][1]['baz']) && is_array($foo['bar'][1])

Where the root is_array may not be necessary if you know it's an array
already (such as in a superglobal or other type-hinted syntax)...



Looking at this deeper, perhaps a notice is indeed the best course of
action.  Otherwise it would call code to behave in a non-obvious and
hard to track down way.  I know it doesn't immediately see obvious to
me that array('foo') should pass isset($array[0]['bar']['baz'])...  I
understand and agree with the change, just the non-obvious nature can
(and likely will) lead to a host of odd bugs.  But if it noticed, then
we would at least have an idea where the oddity happened...

Anthony

On Thu, Nov 24, 2011 at 10:07 PM, Daniel Convissor
<dani...@analysisandsolutions.com> wrote:
> Hi Anthony:
>
>> isset($foo['bar'][1]['baz']) && is_array($foo['bar'][1])
>>
>> You don't need to check each level.  Only the one above the key you're
>> looking at.
>

>
> <?php
> function test2($id, $foo) {
>    echo "ID $id -- ";
>    var_dump(is_array($foo)
>             && isset($foo['bar'][1]['baz']) && is_array($foo['bar'][1]));
>
>    // The following goes fatal on test 80.
>    //var_dump(isset($foo['bar'][1]['baz']) && is_array($foo['bar'][1]));
> }
> test2(10, array('bar' => array(1 => array('baz' => 'string'))));
> test2(20, array('bar' => array(1 => array('boo' => 'string'))));
> test2(30, array('bar' => array(1 => 'string')));
> test2(40, array('bar' => array(2 => 'string')));
> test2(50, array('boo' => array(1 => 'string')));
> test2(60, array('bar' => 'string'));
> test2(70, array('boo' => 'string'));
> test2(80, 'string');
> ?>
>
> Thanks,
>
> --Dan
>
> --
>  T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
>            data intensive web and database programming
>                http://www.AnalysisAndSolutions.com/
>  4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
>

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to