[php-src] Issue #15697: Change in String Offset Access Causes Problems
| From: | PHP-User | Date: | Sun, 01 Sep 2024 20:16:28 +0000 |
| Subject: | [php-src] Issue #15697: Change in String Offset Access Causes Problems | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247670@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15697
Author: PHP-User
### Description
In PHP 7.4, there was a change in the way offsets are accessed in strings. Previously, curly braces
were used ($a{0}), but now square braces are used ($a[0]). This change introduces several issues:
1. Type Ambiguity: With the new syntax ($a[0]), it is not possible to visually determine whether a
variable is an array or a string, which can lead to programming errors.
2. The main issue - problems with the isset() function: The isset function is now practically
unusable for checking the existence of elements in (multidimensional) arrays. For example:
If $a = array('a'), isset($a[0]) returns true, however, if $a = 'a',
isset($a[0]) also returns true. So, does the first element of the array exist or not?
If $a = 'a', isset($a[0][0][0]) also returns true, despite the fact that the variable is
not a multidimensional array, but a string.
When testing the existence of an element in a 3D array, it is now necessary to manually check if
each level of the 3D array is an array, if it is not empty, and if the element exists within it.
From my perspective, this can be extremely impractical and time-consuming.
Questions:
Was the change in string offset access justified, given the problems it introduces?
What is the best way to check for the existence of the element $a[0][0][0] in a 3D array considering
this change?
Thank you.
### PHP Version
PHP 8.3.10
### Operating System
Windows 10 Home