I can't explain this [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

I can't explain this [solved]

Post by josh »

I have some code that runs as part of an automated test. When I run that code in browser I get E_NOTICE errors, I want to reproduce that behavior in test.

Code: Select all

 
var_dump( $this->value );
        var_dump( $this->value['types'] );
        var_dump( isset( $this->value['types'] ) );
        var_dump( count( $this->value['types'] ) );
 
When I run this code in the test I get:
NULL NULL false int(0)
But no errors (I get errors on "line 2" if I run in the browser.)

If I put count( $foo) instead of the above code it does trigger an E_NOTICE error.

Hmm ok I figured it out but still posting anyways. Turns out

Code: Select all

 
$foo = null;
count( $foo['bar'] )
 
is Valid!

.. Where as..

Code: Select all

 
$foo = array();
count( $foo['bar'] )
 
Will blow up your application in your face
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: I can't explain this [solved]

Post by jackpf »

But...$foo['bar'] doesn't exist, nor is it an array.


EDIT
Oh wait...just saw your example with null. Yeah that is odd :P
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: I can't explain this [solved]

Post by josh »

Yeah I opened it as a PHP bug and they called bogus.

First they said by accessing it I was somehow creating it?? but that is obviously wrong because I can see $foo is NULL the whole time in that first example

Turns out the "feature" I discovered was
Note: Accessing variables of other types using [] or {} silently returns NULL.
I would just love to see a programmer for a non open-source project try to pass something like that off as not a bug. Silent failures on strings I could understand, but in what possible situation would I have intended to count() on an array index of NULL
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: I can't explain this [solved]

Post by jackpf »

Yeah, that doesn't make sense. Trying to access an array value of a type that doesn't support it should throw a fatal error imo.

Oh well...
Post Reply