1--TEST--
2Test strncmp() function : usage variations - different inputs(all types)
3--FILE--
4<?php
5/* Prototype  : int strncmp ( string $str1, string $str2, int $len );
6 * Description: Binary safe case-sensitive string comparison of the first n characters
7 * Source code: Zend/zend_builtin_functions.c
8*/
9
10/* Test strncmp() function with the input strings are of all types */
11
12echo "*** Testing strncmp() function: by supplying all types for 'str1' and 'str2' ***\n";
13/* get an unset variable */
14$unset_var = 'string_val';
15unset($unset_var);
16
17/* get resource handle */
18$file_handle = fopen(__FILE__, "r");
19
20/* declaring a class */
21class sample  {
22  public function __toString() {
23  return "object";
24  }
25}
26
27
28/* array with different values */
29$values =  array (
30  /* integer values */
31  0,
32  1,
33  12345,
34  -2345,
35
36  /* float values */
37  10.5,
38  -10.5,
39  10.5e10,
40  10.6E-10,
41  .5,
42
43  /* hexadecimal values */
44  0x12,
45  -0x12,
46
47  /* octal values */
48  012,
49  -012,
50  01.2,
51
52  /* array values */
53  array(),
54  array(0),
55  array(1),
56  array(1, 2),
57  array('color' => 'red', 'item' => 'pen'),
58
59  /* boolean values */
60  true,
61  false,
62  TRUE,
63  FALSE,
64
65  /* nulls */
66  NULL,
67  null,
68
69  /* empty string */
70  "",
71  '',
72
73  /* undefined variable */
74  $undefined_var,
75
76  /* unset variable */
77  $unset_var,
78
79  /* resource */
80  $file_handle,
81
82  /* object */
83  new sample()
84);
85
86/* loop through each element of the array and check the working of strncmp() */
87$counter = 1;
88for($index = 0; $index < count($values); $index ++) {
89  echo "-- Iteration $counter --\n";
90  $str1 = $values[$index];
91  $str2 = $values[$index];
92  $len = strlen($values[$index]) + 1;
93  var_dump( strncmp($str1, $str2, $len) );
94  $counter ++;
95}
96fclose($file_handle);
97
98echo "*** Done ***\n";
99?>
100--EXPECTF--
101*** Testing strncmp() function: by supplying all types for 'str1' and 'str2' ***
102
103Notice: Undefined variable: undefined_var in %s on line %d
104
105Notice: Undefined variable: unset_var in %s on line %d
106-- Iteration 1 --
107int(0)
108-- Iteration 2 --
109int(0)
110-- Iteration 3 --
111int(0)
112-- Iteration 4 --
113int(0)
114-- Iteration 5 --
115int(0)
116-- Iteration 6 --
117int(0)
118-- Iteration 7 --
119int(0)
120-- Iteration 8 --
121int(0)
122-- Iteration 9 --
123int(0)
124-- Iteration 10 --
125int(0)
126-- Iteration 11 --
127int(0)
128-- Iteration 12 --
129int(0)
130-- Iteration 13 --
131int(0)
132-- Iteration 14 --
133int(0)
134-- Iteration 15 --
135
136Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
137
138Warning: strncmp() expects parameter 1 to be string, array given in %s on line %d
139NULL
140-- Iteration 16 --
141
142Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
143
144Warning: strncmp() expects parameter 1 to be string, array given in %s on line %d
145NULL
146-- Iteration 17 --
147
148Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
149
150Warning: strncmp() expects parameter 1 to be string, array given in %s on line %d
151NULL
152-- Iteration 18 --
153
154Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
155
156Warning: strncmp() expects parameter 1 to be string, array given in %s on line %d
157NULL
158-- Iteration 19 --
159
160Warning: strlen() expects parameter 1 to be string, array given in %s on line %d
161
162Warning: strncmp() expects parameter 1 to be string, array given in %s on line %d
163NULL
164-- Iteration 20 --
165int(0)
166-- Iteration 21 --
167int(0)
168-- Iteration 22 --
169int(0)
170-- Iteration 23 --
171int(0)
172-- Iteration 24 --
173int(0)
174-- Iteration 25 --
175int(0)
176-- Iteration 26 --
177int(0)
178-- Iteration 27 --
179int(0)
180-- Iteration 28 --
181int(0)
182-- Iteration 29 --
183int(0)
184-- Iteration 30 --
185
186Warning: strlen() expects parameter 1 to be string, resource given in %s on line %d
187
188Warning: strncmp() expects parameter 1 to be string, resource given in %s on line %d
189NULL
190-- Iteration 31 --
191int(0)
192*** Done ***
193