- URL: https://www.laruence.com/en/2011/12/19/2409.html
- Please include attribution when republishing.
Previously, I introduced an important change in PHP 5.4 (Chained string offsets). Afterwards, since most people said this change is subtle and easy to fall into, we have now made some improvements to it.
The specific change: for a variable $a, if $a is a string, then for a non-numeric index, e.g. $a["foo"], isset will return false and empty will return true. But to stay compatible with existing code, when you read this value it will still return $a[0], while additionally emitting a warning. For example:
<?php $a = "laruence"; var_dump($a["foo"]) ; //PHP Warning: Illegal string offset 'foo' //output string(1) "l" var_dump(isset($a["foo"])); //false var_dump(empty($a["foo"])); //true
For the cases where the key is a bool, double or null, the behavior stays the same as before, but a Notice will be emitted.
<?php $a = "bar"; echo $a[TRUE]; //PHP Notice: String offset cast occured //output a
And for numeric string indexes like "1", "12", etc., the behavior stays the same as before.
That said, a reminder: PHP 5.4 is still under development. Before the final release, any new feature may be adjusted or changed. If you have any suggestions, feedback is welcome, to help make PHP even better.
Thanks
For more updates, follow: Changelog
Be First to Comment