Folks:

I came across a subtle bug a developer introduced into our application.  
It took us a month to realize the bug was there because PHP didn't throw a 
notice.  Here is a simplified version of what was happening.

    // function some_func() {}
    $a = some_func();
    if ($a['do_something'] == true) {
       // Do it.
    }

some_func() was supposed to return an array.  But it was returning void.  
So $a was NULL.  Oops -- we all make mistakes.  What's unfortunate is PHP 
didn't raise a "Notice: Undefined index: do_something" here.  It would 
have saved us some headaches.  I'm sure others have run into this as well.

The following also doesn't produce a notice:

    $a = 12;
    echo $a['k']

I looked through the bugs database and mailing list archive and came up 
with nothing on this particular issue.  But bugs 29271, 30885 and 38165 
cover the situation where a key's string is auto-converted to 0:

    // While this is a behavior we all truly expect:
    $a = 'value';
    echo $a[0] . "\n";  // output: v

    // Another oddity, but people closing bugs say it's expected:
    $a = 'value';
    echo $a['k'] . "\n";  // output: v

This last behavior is counter-intuitive, let alone un-documented.

Wondering what the folks here think about this.

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