xref: /PHP-7.4/ext/standard/tests/strings/strlen.phpt (revision be6f1f68)
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 Miscelleneous 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 stringable {
67  function __toString() {
68    return "Hello, world";
69  }
70}
71$obj_string = new stringable;
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));
79var_dump(strlen("$str_arr[1]"));
80var_dump(strlen("$str_arr[2]"));
81
82
83echo "\n--- Testing a longer and heredoc string ---\n";
84$string = <<<EOD
85abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
86abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
87abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
88abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
89abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
90abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
91abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
92@#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
93abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
94EOD;
95var_dump(strlen($string));
96
97echo "\n--- Testing a heredoc null string ---\n";
98$str = <<<EOD
99EOD;
100var_dump(strlen($str));
101
102
103echo "\n--- Testing simple and complex syntax strings ---\n";
104$str = 'world';
105
106/* Simple syntax */
107var_dump(strlen("$str"));
108var_dump(strlen("$str'S"));
109var_dump(strlen("$strS"));
110
111/* String with curly braces, complex syntax */
112var_dump(strlen("${str}S"));
113var_dump(strlen("{$str}S"));
114
115echo "\n--- strlen for long float values ---\n";
116/* Here two different outputs, which depends on the rounding value
117   before converting to string. Here Precision = 12  */
118var_dump(strlen(10.55555555555555555555555555));   		// len = 13
119var_dump(strlen(10.55555555595555555555555555));    		// len = 12
120
121echo "\n--- Nested strlen() ---\n";
122var_dump(strlen(strlen("Hello"))); 				// len=1
123
124
125echo "\n#### error conditions ####";
126/* Zero arguments */
127strlen();
128/* Greater number of args than expected */
129strlen("string1", "string2");
130strlen("", "");
131
132echo "Done\n";
133?>
134--EXPECTF--
135#### Basic operations and  variations ####
136String length of 'Hello, World' is => int(12)
137String length of 'Hello, World' is => int(12)
138String length of '!!Hello, World' is => int(14)
139String length of '??Hello, World' is => int(14)
140String length of '$@#%^&*!~,.:;?' is => int(14)
141String length of '123' is => int(3)
142String length of '123' is => int(3)
143String length of '-1.2345' is => int(7)
144String length of '-1.2344' is => int(7)
145String length of '' is => int(0)
146String length of '' is => int(0)
147String length of ' ' is => int(1)
148String length of '�' is => int(1)
149String length of '�0' is => int(2)
150String length of '�C' is => int(2)
151String length of '�0' is => int(2)
152String length of '0' is => int(1)
153String length of '0' is => int(1)
154String length of '	' is => int(1)
155String length of '\t' is => int(2)
156String length of '1' is => int(1)
157String length of '' is => int(0)
158String length of 'Hello, World�' is => int(13)
159String length of 'Hello�World' is => int(11)
160String length of 'Hello, World\0' is => int(14)
161String length of 'Hello, World
162' is => int(13)
163String length of 'Hello, World
163' is => int(13)
164String length of 'Hello, World	' is => int(13)
165String length of 'Hello, World\' is => int(13)
166String length of '              ' is => int(14)
167String length of '��A��' is => int(5)
168String length of 'abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
169                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
170                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
171                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
172                   abcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*()_+=|?><-;:$
173                   []{}{{{}}}[[[[]][]]]***&&&^^%$###@@!!@#$%&^&**/////|\\\
174                   abcdefghijklmnopqrstuvwxyz0123456789' is => int(495)
175
176#### Testing Miscelleneous inputs ####
177--- Testing objects ---
178int(12)
179
180--- Testing arrays ---
181
182Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
183NULL
184int(6)
185int(20)
186
187--- Testing a longer and heredoc string ---
188int(639)
189
190--- Testing a heredoc null string ---
191int(0)
192
193--- Testing simple and complex syntax strings ---
194int(5)
195int(7)
196
197Notice: Undefined variable: strS in %s on line %d
198int(0)
199int(6)
200int(6)
201
202--- strlen for long float values ---
203int(13)
204int(12)
205
206--- Nested strlen() ---
207int(1)
208
209#### error conditions ####
210Warning: strlen() expects exactly 1 parameter, 0 given in %s on line %d
211
212Warning: strlen() expects exactly 1 parameter, 2 given in %s on line %d
213
214Warning: strlen() expects exactly 1 parameter, 2 given in %s on line %d
215Done
216