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