1--TEST-- 2Test strrchr() function : usage variations - unexpected inputs for needle 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 needle 11 * and expected type for haystack 12*/ 13 14echo "*** Testing strrchr() function with unexpected inputs for needle ***\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$haystacks = array ( 31 //integer numeric strings 32 "0", 33 "1", 34 "2", 35 "-2", 36 37 //float numeric strings 38 "10.5", 39 "-10.5", 40 "10.5e10", 41 "10.6E-10", 42 ".5", 43 44 //regular strings 45 "array", 46 "a", 47 "r", 48 "y", 49 "ay", 50 "true", 51 "false", 52 "TRUE", 53 "FALSE", 54 "NULL", 55 "null", 56 "object", 57 58 //empty string 59 "", 60 '', 61 62 //resource variable in string form 63 "\$file_handle", 64 65 //undefined variable in string form 66 @"$undefined_var", 67 @"$unset_var" 68); 69 70// array with different values 71$needles = array ( 72 73 // integer values 74 0, 75 1, 76 12345, 77 -2345, 78 79 // float values 80 10.5, 81 -10.5, 82 10.1234567e10, 83 10.7654321E-10, 84 .5, 85 86 // array values 87 array(), 88 array(0), 89 array(1), 90 array(1, 2), 91 array('color' => 'red', 'item' => 'pen'), 92 93 // boolean values 94 true, 95 false, 96 TRUE, 97 FALSE, 98 99 // null vlaues 100 NULL, 101 null, 102 103 // objects 104 new sample(), 105 106 // empty string 107 "", 108 '', 109 110 // resource 111 $file_handle, 112 113 // undefined variable 114 @$undefined_var, 115 116 // unset variable 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 needle *** 134-- Iteration 1 -- 135bool(false) 136-- Iteration 2 -- 137bool(false) 138-- Iteration 3 -- 139bool(false) 140-- Iteration 4 -- 141bool(false) 142-- Iteration 5 -- 143bool(false) 144-- Iteration 6 -- 145bool(false) 146-- Iteration 7 -- 147bool(false) 148-- Iteration 8 -- 149bool(false) 150-- Iteration 9 -- 151bool(false) 152-- Iteration 10 -- 153 154Warning: strrchr(): needle is not a string or an integer in %s on line %d 155bool(false) 156-- Iteration 11 -- 157 158Warning: strrchr(): needle is not a string or an integer in %s on line %d 159bool(false) 160-- Iteration 12 -- 161 162Warning: strrchr(): needle is not a string or an integer in %s on line %d 163bool(false) 164-- Iteration 13 -- 165 166Warning: strrchr(): needle is not a string or an integer in %s on line %d 167bool(false) 168-- Iteration 14 -- 169 170Warning: strrchr(): needle is not a string or an integer in %s on line %d 171bool(false) 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 -- 185 186Notice: Object of class sample could not be converted to int in %s on line %d 187bool(false) 188-- Iteration 22 -- 189bool(false) 190-- Iteration 23 -- 191bool(false) 192-- Iteration 24 -- 193 194Warning: strrchr(): needle is not a string or an integer in %s on line %d 195bool(false) 196-- Iteration 25 -- 197bool(false) 198-- Iteration 26 -- 199bool(false) 200*** Done *** 201