php-src/Zend/tests/numeric_strings/string_offset.phpt
George Peter Banyard b2248789ed Implement 'Saner Numeric Strings' RFC:
RFC: https://wiki.php.net/rfc/saner-numeric-strings

This removes the -1 allow_error mode from is_numeric_string functions and replaces it by
a trailing boolean out argument to preserve BC in a couple of places.

Most of the changes can be resumed to "numeric" strings which emitted a E_NOTICE now emit
a E_WARNING and "numeric" strings which emitted a E_WARNING now throw a TypeError.

This mostly affects:
 - String offsets
 - Arithmetic operations
 - Bitwise operations

Closes GH-5762
2020-07-29 02:51:09 +01:00

73 lines
1.4 KiB
PHP

--TEST--
Using different sorts of numerical strings as a string offset
--FILE--
<?php
$str = "The world is fun";
$keys = [
"7",
"7.5",
" 7",
" 7.5",
" 7 ",
" 7.5 ",
"7 ",
"7.5 ",
"7str",
"7.5str",
" 7str",
" 7.5str",
" 7 str",
" 7.5 str",
"7 str",
"7.5 str",
"0xC",
"0b10",
"07",
];
foreach ($keys as $key) {
try {
var_dump($str[$key]);
} catch (\TypeError $e) {
echo $e->getMessage() . \PHP_EOL;
}
}
echo "Done\n";
?>
--EXPECTF--
string(1) "l"
Cannot access offset of type string on string
string(1) "l"
Cannot access offset of type string on string
string(1) "l"
Cannot access offset of type string on string
string(1) "l"
Cannot access offset of type string on string
Warning: Illegal string offset "7str" in %s on line %d
string(1) "l"
Cannot access offset of type string on string
Warning: Illegal string offset " 7str" in %s on line %d
string(1) "l"
Cannot access offset of type string on string
Warning: Illegal string offset " 7 str" in %s on line %d
string(1) "l"
Cannot access offset of type string on string
Warning: Illegal string offset "7 str" in %s on line %d
string(1) "l"
Cannot access offset of type string on string
Warning: Illegal string offset "0xC" in %s on line %d
string(1) "T"
Warning: Illegal string offset "0b10" in %s on line %d
string(1) "T"
string(1) "l"
Done