How does the “Array dereferencing” work on a scalar value of type boolean/integer/float/string as of PHP version 7.2.0?

后端 未结 1 1384
攒了一身酷
攒了一身酷 2021-01-07 00:11

I am using PHP 7.2. I come across the following note from the arrays chapter of PHP Manual

Array dereferencing a scalar value which is not a string s

1条回答
  •  天涯浪人
    2021-01-07 01:02

    They are referring to non-complex types such as int or float.

    In your example you are using an array. So you don't see the issue.

    The first pair of brackets is array-dereferencing on the array (1, 2222, 3), the second is array-dereferencing on an integer (2222) which always returns null.

    Simplified:

    This "fails silently" as in theory you should get an error from this, rather than just null.

    Also happens with null, other than int, float, bool:

    But works correctly instead with arrays and strings.

    Answering your question, "How does the “Array dereferencing” work on a scalar value of type": It doesn't, it just returns null instead of returning an error of some sort.

    0 讨论(0)
提交回复
热议问题