php-src/Zend/tests/str_offset_004.phpt
George Peter Banyard bfe3f934a3 Add warning and convert to exception in string offset assignment:
Convert the empty string assignment to an Error as per RFC [1]
Add a warning that only the first byte will be assigned to the offset if provided
a needle that is longer than one byte.

[1] https://wiki.php.net/rfc/engine_warnings
2020-01-07 21:54:42 +01:00

52 lines
966 B
PHP

--TEST--
string offset 004
--FILE--
<?php
// Test assignments using (positive and negative) string offsets
$str = "abcdefghijklmno";
$i = 3;
$j = -4;
$str[2] = 'C';
var_dump($str);
$str[$i] = 'Z';
var_dump($str);
$str[-5] = 'P';
var_dump($str);
$str[$j] = 'Q';
var_dump($str);
$str[-20] = 'Y';
var_dump($str);
$str[-strlen($str)] = strtoupper($str[0]); /* An exotic ucfirst() ;) */
var_dump($str);
$str[20] = 'N';
var_dump($str);
$str[-2] = 'UFO';
var_dump($str);
$str[-$i] = $str[$j*2];
var_dump($str);
?>
--EXPECTF--
string(15) "abCdefghijklmno"
string(15) "abCZefghijklmno"
string(15) "abCZefghijPlmno"
string(15) "abCZefghijPQmno"
Warning: Illegal string offset: -20 in %sstr_offset_004.php on line %d
string(15) "abCZefghijPQmno"
string(15) "AbCZefghijPQmno"
string(21) "AbCZefghijPQmno N"
Warning: Only the first byte will be assigned to the string offset in %s on line %d
string(21) "AbCZefghijPQmno UN"
string(21) "AbCZefghijPQmno nUN"