Why does PHP not complain when I treat a null value as an array like this?

后端 未结 3 1664
你的背包
你的背包 2020-12-01 10:11

In PHP, I have error_reporting set to report everything including notices.

Why does the following not throw any notices, errors or anything else?

$my         


        
3条回答
  •  眼角桃花
    2020-12-01 10:59

    Usually, when you try to use a value of one type as if it were another type, either an error or warning gets thrown or "type juggling" takes place. For example, if you try to concatenate two numbers with ., they'll both get coerced to strings and concatenated.

    However, as explained on the manual page about type juggling, this isn't the case when treating a non-array like an array:

    The behaviour of an automatic conversion to array is currently undefined.

    In practice, the behaviour that happens when this "undefined behaviour" is triggered by dereferencing a non-array is that null gets returned, as you've observed. This doesn't just affect nulls - you'll also get null if you try to dereference a number or a resource.

提交回复
热议问题