1

I recently stumbled across the following:

<? $d=false; var_dump($d[123]); ?>

which yielded NULL, but (unexpected to me) without any notice, as for example

<? $d=array(); var_dump($d[123]); ?>

does produce the well known

Notice: Uninitialized string offset:  123 in - on line 1

What is going on here? Is there any documentation of this behavior?

1
  • 1
    That's pretty sad behaviour, if you ask me. Commented Mar 24, 2011 at 23:33

3 Answers 3

4

From here:

Accessing variables of other types (not including arrays or objects implementing the appropriate interfaces) using [] or {} silently returns NULL.

Sign up to request clarification or add additional context in comments.

Comments

2

I guess that is what you would call undefined behaviour.

It makes sense, as a boolean can not have an offset.

error_reporting(E_ALL);
$d = TRUE; 
var_dump($d[0]);

This also produces NULL (if FALSE was coerced to an empty string, it would make sense that TRUE would be 1).

Plus what meze said :)

Comments

1

Quick look at the source code shows that this is expected behavior. But don't ask me why they did it this way...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.