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