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