xref: /PHP-8.1/Zend/tests/bug53432.phpt (revision 418fcd22)
1--TEST--
2Bug #53432: Assignment via string index access on an empty string converts to array
3--FILE--
4<?php
5
6$str = '';
7var_dump($str[0] = 'a');
8var_dump($str);
9
10$str = '';
11var_dump($str[5] = 'a');
12var_dump($str);
13
14$str = '';
15var_dump($str[-1] = 'a');
16var_dump($str);
17
18$str = '';
19try {
20    var_dump($str['foo'] = 'a');
21} catch (\TypeError $e) {
22    echo $e->getMessage() . \PHP_EOL;
23}
24var_dump($str);
25
26$str = '';
27try {
28    var_dump($str[] = 'a');
29} catch (Error $e) {
30    echo "Error: {$e->getMessage()}\n";
31}
32var_dump($str);
33
34$str = '';
35try {
36    var_dump($str[0] += 1);
37} catch (Error $e) {
38    echo "Error: {$e->getMessage()}\n";
39}
40var_dump($str);
41
42$str = '';
43try {
44    var_dump($str[0][0] = 'a');
45} catch (Error $e) {
46    echo "Error: {$e->getMessage()}\n";
47}
48var_dump($str);
49
50?>
51--EXPECTF--
52string(1) "a"
53string(1) "a"
54string(1) "a"
55string(6) "     a"
56
57Warning: Illegal string offset -1 in %s on line %d
58NULL
59string(0) ""
60Cannot access offset of type string on string
61string(0) ""
62Error: [] operator not supported for strings
63string(0) ""
64Error: Cannot use assign-op operators with string offsets
65string(0) ""
66Error: Cannot use string offset as an array
67string(0) ""
68