Skip to content

Commit eb4bde8

Browse files
authored
Split the illegal string offsets example output by PHP version (#5232)
The output shown for the example was already corrected in master; what the issue thread still asked for was making the version difference visible. Split the output into two sections with example.outputs.8 and example.outputs.7, and state the PHP 8 rule in the introductory paragraph. State the PHP 8 rule as it actually is. A string starting with an integer followed by other characters, such as "1x", does not throw a TypeError: it still emits E_WARNING and is read as its leading integer, which the PHP 8 output of this very example shows. Only the other non-integer-like strings throw. The PHP 7 output is what PHP 7 really prints: isset() rejects every offset that is not integer-like, so it is false for both '1.0' and '1x', and '1x' emits "A non well formed numeric value encountered" rather than an illegal offset warning, since the read path allows trailing data. Sources: - Zend/zend_execute.c, zend_isset_dim_slow() and the string branch of zend_fetch_dimension_address_read(); Zend/zend_operators.c, _is_numeric_string_ex() - Zend/tests/offset_string.phpt and Zend/tests/isset_str_offset.phpt in 7.4 - PHP 8 output verified against PHP 8.5.4
1 parent 5a91c9a commit eb4bde8

1 file changed

Lines changed: 33 additions & 6 deletions

File tree

language/types/string.xml

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,10 +1088,15 @@ var_dump($str);
10881088
</programlisting>
10891089
</example>
10901090

1091-
<para>
1092-
String offsets have to either be integers or integer-like strings,
1093-
otherwise a warning will be thrown.
1094-
</para>
1091+
<simpara>
1092+
String offsets must be either integers or integer-like strings.
1093+
As of PHP 8.0.0, any other string throws a
1094+
<exceptionname>TypeError</exceptionname>, except for a string starting
1095+
with an integer followed by other characters, which emits
1096+
<constant>E_WARNING</constant> and is interpreted as that leading integer.
1097+
Prior to PHP 8.0.0, no <exceptionname>TypeError</exceptionname> was
1098+
thrown: an illegal offset emitted <constant>E_WARNING</constant> instead.
1099+
</simpara>
10951100

10961101
<example>
10971102
<title>Example of Illegal String Offsets</title>
@@ -1116,7 +1121,7 @@ foreach ($keys as $keyToTry) {
11161121
?>
11171122
]]>
11181123
</programlisting>
1119-
&example.outputs;
1124+
&example.outputs.8;
11201125
<screen>
11211126
<![CDATA[
11221127
bool(true)
@@ -1130,7 +1135,29 @@ Cannot access offset of type string on string
11301135
11311136
bool(false)
11321137
1133-
Warning: Illegal string offset "1x" in Standard input code on line 10
1138+
Warning: Illegal string offset "1x" in example.php on line 10
1139+
string(1) "b"
1140+
]]>
1141+
</screen>
1142+
&example.outputs.7;
1143+
<screen>
1144+
<![CDATA[
1145+
bool(true)
1146+
string(1) "b"
1147+
1148+
bool(false)
1149+
1150+
Warning: Illegal string offset '1.0' in example.php on line 10
1151+
string(1) "b"
1152+
1153+
bool(false)
1154+
1155+
Warning: Illegal string offset 'x' in example.php on line 10
1156+
string(1) "a"
1157+
1158+
bool(false)
1159+
1160+
Notice: A non well formed numeric value encountered in example.php on line 10
11341161
string(1) "b"
11351162
]]>
11361163
</screen>

0 commit comments

Comments
 (0)