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