xref: /PHP-7.3/Zend/tests/bug53432.phpt (revision 766ad0d9)
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 = '';
19var_dump($str['foo'] = 'a');
20var_dump($str);
21
22$str = '';
23try {
24    var_dump($str[] = 'a');
25} catch (Error $e) {
26    echo "Error: {$e->getMessage()}\n";
27}
28var_dump($str);
29
30$str = '';
31try {
32    var_dump($str[0] += 1);
33} catch (Error $e) {
34    echo "Error: {$e->getMessage()}\n";
35}
36var_dump($str);
37
38$str = '';
39try {
40    var_dump($str[0][0] = 'a');
41} catch (Error $e) {
42    echo "Error: {$e->getMessage()}\n";
43}
44var_dump($str);
45
46?>
47--EXPECTF--
48string(1) "a"
49string(1) "a"
50string(1) "a"
51string(6) "     a"
52
53Warning: Illegal string offset:  -1 in %s on line %d
54NULL
55string(0) ""
56
57Warning: Illegal string offset 'foo' in %s on line %d
58string(1) "a"
59string(1) "a"
60Error: [] operator not supported for strings
61string(0) ""
62Error: Cannot use assign-op operators with string offsets
63string(0) ""
64Error: Cannot use string offset as an array
65string(0) ""
66