xref: /PHP-8.1/ext/standard/tests/strings/strlen.phpt (revision ea256a21)
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--
125#### Basic operations and  variations ####
126String length of 'Hello, World' is => int(12)
127String length of 'Hello, World' is => int(12)
128String length of '!!Hello, World' is => int(14)
129String length of '??Hello, World' is => int(14)
130String length of '$@#%^&*!~,.:;?' is => int(14)
131String length of '123' is => int(3)
132String length of '123' is => int(3)
133String length of '-1.2345' is => int(7)
134String length of '-1.2344' is => int(7)
135String length of '' is => int(0)
136String length of ' ' is => int(1)
137String length of '%0' is => int(1)
138String length of '%00' is => int(2)
139String length of '�C' is => int(2)
140String length of '%00' is => int(2)
141String length of '0' is => int(1)
142String length of '0' is => int(1)
143String length of '	' is => int(1)
144String length of '\t' is => int(2)
145String length of '1' is => int(1)
146String length of '' is => int(0)
147String length of 'Hello, World%0' is => int(13)
148String length of 'Hello%0World' is => int(11)
149String length of 'Hello, World\0' is => int(14)
150String length of 'Hello, World
151' is => int(13)
152String length of 'Hello, World
152' is => int(13)
153String length of 'Hello, World	' is => int(13)
154String length of 'Hello, World\' is => int(13)
155String length of '              ' is => int(14)
156String length of '��A�%0' is => int(5)
157String length of 'abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
158                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
159                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
160                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
161                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
162                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
163                   abcdefghijklmnopqrstuvwxyz0123456789' is => int(495)
164
165#### Testing miscellaneous inputs ####
166--- Testing objects ---
167int(12)
168
169--- Testing arrays ---
170int(6)
171int(20)
172
173--- Testing a longer and heredoc string ---
174int(639)
175
176--- Testing a heredoc null string ---
177int(0)
178
179--- Testing simple and complex syntax strings ---
180int(5)
181int(7)
182
183Warning: Undefined variable $strS in %s on line %d
184int(0)
185int(6)
186int(6)
187
188--- strlen for long float values ---
189int(13)
190int(12)
191
192--- Nested strlen() ---
193int(1)
194Done
195