1--TEST--
2Test strrpos() function : usage variations - unexpected inputs for 'offset' argument
3--FILE--
4<?php
5/* Prototype  : int strrpos ( string $haystack, string $needle [, int $offset] );
6 * Description: Find position of last occurrence of 'needle' in 'haystack'.
7 * Source code: ext/standard/string.c
8*/
9
10/* Test strrpos() function with unexpected inputs for 'offset' argument */
11
12echo "*** Testing strrpos() function: with unexpected values for offset ***\n";
13
14// get an unset variable
15$unset_var = 'string_val';
16unset($unset_var);
17
18// defining a class
19class sample  {
20  public function __toString() {
21    return "object";
22  }
23}
24
25//getting the resource
26$file_handle = fopen(__FILE__, "r");
27
28//definition of input args
29$haystack = "hello world";
30$needle = "world";
31
32// array with different values
33$offsets =  array (
34
35  // float values
36  1.5,
37  -1.5,
38  1.5e10,
39  1.6E-10,
40  .5,
41
42  // array values
43  array(),
44  array(0),
45  array(1),
46  array(1, 2),
47  array('color' => 'red', 'item' => 'pen'),
48
49  // boolean values
50  true,
51  false,
52  TRUE,
53  FALSE,
54
55  // objects
56  new sample(),
57
58  // empty string
59  "",
60  '',
61
62  // null vlaues
63  NULL,
64  null,
65
66  //resource
67  $file_handle,
68
69  // undefined variable
70  @$undefined_var,
71
72  // unset variable
73  @$unset_var
74);
75
76
77// loop through each element of the array and check the working of strrpos()
78$counter = 1;
79for($index = 0; $index < count($offsets); $index ++) {
80  echo "-- Iteration $counter --\n";
81  var_dump( strrpos($haystack, $needle, $offsets[$index]) );
82  $counter ++;
83}
84
85echo "*** Done ***";
86?>
87--EXPECTF--
88*** Testing strrpos() function: with unexpected values for offset ***
89-- Iteration 1 --
90int(6)
91-- Iteration 2 --
92int(6)
93-- Iteration 3 --
94
95Warning: strrpos(): Offset is greater than the length of haystack string in %s on line %d
96bool(false)
97-- Iteration 4 --
98int(6)
99-- Iteration 5 --
100int(6)
101-- Iteration 6 --
102
103Warning: strrpos() expects parameter 3 to be long, array given in %s on line %d
104bool(false)
105-- Iteration 7 --
106
107Warning: strrpos() expects parameter 3 to be long, array given in %s on line %d
108bool(false)
109-- Iteration 8 --
110
111Warning: strrpos() expects parameter 3 to be long, array given in %s on line %d
112bool(false)
113-- Iteration 9 --
114
115Warning: strrpos() expects parameter 3 to be long, array given in %s on line %d
116bool(false)
117-- Iteration 10 --
118
119Warning: strrpos() expects parameter 3 to be long, array given in %s on line %d
120bool(false)
121-- Iteration 11 --
122int(6)
123-- Iteration 12 --
124int(6)
125-- Iteration 13 --
126int(6)
127-- Iteration 14 --
128int(6)
129-- Iteration 15 --
130
131Warning: strrpos() expects parameter 3 to be long, object given in %s on line %d
132bool(false)
133-- Iteration 16 --
134
135Warning: strrpos() expects parameter 3 to be long, string given in %s on line %d
136bool(false)
137-- Iteration 17 --
138
139Warning: strrpos() expects parameter 3 to be long, string given in %s on line %d
140bool(false)
141-- Iteration 18 --
142int(6)
143-- Iteration 19 --
144int(6)
145-- Iteration 20 --
146
147Warning: strrpos() expects parameter 3 to be long, resource given in %s on line %d
148bool(false)
149-- Iteration 21 --
150int(6)
151-- Iteration 22 --
152int(6)
153*** Done ***
154