xref: /PHP-8.3/ext/standard/tests/strings/strlen.phpt (revision 9a90bd70)
1--TEST--
2strlen() function
3--INI--
4precision = 12
5--FILE--
6<?php
7/* returns the length of a given string */
8
9echo "#### Basic operations and  variations ####\n";
10$strings = array( "Hello, World",
11          'Hello, World',
12          '!!Hello, World',
13          "??Hello, World",
14          "$@#%^&*!~,.:;?",
15          "123",
16          123,
17          "-1.2345",
18          -1.2344,
19          "",
20          " ",
21          "\0",
22          "\x000",					// len = 2
23          "\xABC",					// len = 2
24          "\0000",					// len = 2
25          "0",
26          0,
27          "\t", 					// len = 1
28          '\t', 					// len = 2
29          TRUE,
30          FALSE,
31          "Hello, World\0",
32          "Hello\0World",
33          'Hello, World\0',
34          "Hello, World\n",
35          "Hello, World\r",
36          "Hello, World\t",
37          "Hello, World\\",
38          "              ",
39          chr(128).chr(234).chr(65).chr(255).chr(256),
40
41          "abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
42                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
43                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
44                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
45                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
46                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\\\\
47                   abcdefghijklmnopqrstuvwxyz0123456789"
48        );
49
50/* loop through to find the length of each string */
51for($i=0; $i<count($strings); $i++) {
52  echo "String length of '$strings[$i]' is => ";
53  var_dump( strlen($strings[$i]) );
54}
55
56
57
58echo "\n#### Testing miscellaneous inputs ####\n";
59
60echo "--- Testing objects ---\n";
61/* we get "Recoverable fatal error: saying Object of class could not be converted
62    to string" by default when an object is passed instead of string:
63The error can be  avoided by choosing the __toString magix method as follows: */
64
65class StringCapable {
66  function __toString() {
67    return "Hello, world";
68  }
69}
70$obj_string = new StringCapable;
71
72var_dump(strlen("$obj_string"));
73
74
75echo "\n--- Testing arrays ---\n";
76$str_arr = array("hello", "?world", "!$%**()%**[][[[&@#~!", array());
77var_dump(strlen("$str_arr[1]"));
78var_dump(strlen("$str_arr[2]"));
79
80
81echo "\n--- Testing a longer and heredoc string ---\n";
82$string = <<<EOD
83abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
84abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
85abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
86abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
87abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
88abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
89abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
90@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
91abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
92EOD;
93var_dump(strlen($string));
94
95echo "\n--- Testing a heredoc null string ---\n";
96$str = <<<EOD
97EOD;
98var_dump(strlen($str));
99
100
101echo "\n--- Testing simple and complex syntax strings ---\n";
102$str = 'world';
103
104/* Simple syntax */
105var_dump(strlen("$str"));
106var_dump(strlen("$str'S"));
107var_dump(strlen("$strS"));
108
109/* String with curly braces, complex syntax */
110var_dump(strlen("${str}S"));
111var_dump(strlen("{$str}S"));
112
113echo "\n--- strlen for long float values ---\n";
114/* Here two different outputs, which depends on the rounding value
115   before converting to string. Here Precision = 12  */
116var_dump(strlen(10.55555555555555555555555555));   		// len = 13
117var_dump(strlen(10.55555555595555555555555555));    		// len = 12
118
119echo "\n--- Nested strlen() ---\n";
120var_dump(strlen(strlen("Hello"))); 				// len=1
121
122echo "Done\n";
123?>
124--EXPECTF--
125Deprecated: Using ${var} in strings is deprecated, use {$var} instead in %s on line %d
126#### Basic operations and  variations ####
127String length of 'Hello, World' is => int(12)
128String length of 'Hello, World' is => int(12)
129String length of '!!Hello, World' is => int(14)
130String length of '??Hello, World' is => int(14)
131String length of '$@#%^&*!~,.:;?' is => int(14)
132String length of '123' is => int(3)
133String length of '123' is => int(3)
134String length of '-1.2345' is => int(7)
135String length of '-1.2344' is => int(7)
136String length of '' is => int(0)
137String length of ' ' is => int(1)
138String length of '%0' is => int(1)
139String length of '%00' is => int(2)
140String length of '�C' is => int(2)
141String length of '%00' is => int(2)
142String length of '0' is => int(1)
143String length of '0' is => int(1)
144String length of '	' is => int(1)
145String length of '\t' is => int(2)
146String length of '1' is => int(1)
147String length of '' is => int(0)
148String length of 'Hello, World%0' is => int(13)
149String length of 'Hello%0World' is => int(11)
150String length of 'Hello, World\0' is => int(14)
151String length of 'Hello, World
152' is => int(13)
153String length of 'Hello, World
153' is => int(13)
154String length of 'Hello, World	' is => int(13)
155String length of 'Hello, World\' is => int(13)
156String length of '              ' is => int(14)
157String length of '��A�%0' is => int(5)
158String length of 'abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
159                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
160                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
161                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
162                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
163                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
164                   abcdefghijklmnopqrstuvwxyz0123456789' is => int(495)
165
166#### Testing miscellaneous inputs ####
167--- Testing objects ---
168int(12)
169
170--- Testing arrays ---
171int(6)
172int(20)
173
174--- Testing a longer and heredoc string ---
175int(639)
176
177--- Testing a heredoc null string ---
178int(0)
179
180--- Testing simple and complex syntax strings ---
181int(5)
182int(7)
183
184Warning: Undefined variable $strS in %s on line %d
185int(0)
186int(6)
187int(6)
188
189--- strlen for long float values ---
190int(13)
191int(12)
192
193--- Nested strlen() ---
194int(1)
195Done
196