xref: /PHP-8.0/Zend/tests/offset_string.phpt (revision f0fd5922)
1--TEST--
2using different variables to access string offsets
3--FILE--
4<?php
5
6$str = "Sitting on a corner all alone, staring from the bottom of his soul";
7
8var_dump($str[1]);
9var_dump($str[0.0836]);
10var_dump($str[NULL]);
11try {
12    var_dump($str["run away"]);
13} catch (\TypeError $e) {
14    echo $e->getMessage() . \PHP_EOL;
15}
16var_dump($str["13"]);
17try {
18    var_dump($str["14.5"]);
19} catch (\TypeError $e) {
20    echo $e->getMessage() . \PHP_EOL;
21}
22var_dump($str["15 and then some"]);
23
24var_dump($str[TRUE]);
25var_dump($str[FALSE]);
26
27$fp = fopen(__FILE__, "r");
28try {
29    var_dump($str[$fp]);
30} catch (Error $e) {
31    echo $e->getMessage(), "\n";
32}
33
34$obj = new stdClass;
35try {
36    var_dump($str[$obj]);
37} catch (Error $e) {
38    echo $e->getMessage(), "\n";
39}
40
41$arr = Array(1,2,3);
42try {
43    var_dump($str[$arr]);
44} catch (Error $e) {
45    echo $e->getMessage(), "\n";
46}
47
48echo "Done\n";
49?>
50--EXPECTF--
51string(1) "i"
52
53Warning: String offset cast occurred in %s on line %d
54string(1) "S"
55
56Warning: String offset cast occurred in %s on line %d
57string(1) "S"
58Cannot access offset of type string on string
59string(1) "c"
60Cannot access offset of type string on string
61
62Warning: Illegal string offset "15 and then some" in %s on line %d
63string(1) "r"
64
65Warning: String offset cast occurred in %s on line %d
66string(1) "i"
67
68Warning: String offset cast occurred in %s on line %d
69string(1) "S"
70Cannot access offset of type resource on string
71Cannot access offset of type stdClass on string
72Cannot access offset of type array on string
73Done
74