1--TEST-- 2Test mb_strrpos() function : usage variations - Pass different data types as $offset arg 3--SKIPIF-- 4<?php 5extension_loaded('mbstring') or die('skip'); 6function_exists('mb_strrpos') or die("skip mb_strrpos() is not available in this build"); 7?> 8--FILE-- 9<?php 10/* Prototype : int mb_strrpos(string $haystack, string $needle [, int $offset [, string $encoding]]) 11 * Description: Find position of last occurrence of a string within another 12 * Source code: ext/mbstring/mbstring.c 13 */ 14 15/* 16 * Pass mb_strrpos() different data types as $offset argument to test behaviour 17 */ 18 19echo "*** Testing mb_strrpos() : usage variations ***\n"; 20 21// Initialise function arguments not being substituted 22$needle = b'a'; 23$haystack = b'string_val'; 24$encoding = 'utf-8'; 25 26//get an unset variable 27$unset_var = 10; 28unset ($unset_var); 29 30// get a class 31class classA 32{ 33 public function __toString() { 34 return b"7"; 35 } 36} 37 38// heredoc string 39$heredoc = b<<<EOT 40hello world 41EOT; 42 43// unexpected values to be passed to $offset argument 44$inputs = array( 45 46 // int data 47/*1*/ 0, 48 1, 49 12345, 50 -2345, 51 52 // float data 53/*5*/ 10.5, 54 -10.5, 55 12.3456789000e10, 56 12.3456789000E-10, 57 .5, 58 59 // null data 60/*10*/ NULL, 61 null, 62 63 // boolean data 64/*12*/ true, 65 false, 66 TRUE, 67 FALSE, 68 69 // object data 70/*16*/ new classA(), 71 72 // undefined data 73/*17*/ @$undefined_var, 74 75 // unset data 76/*18*/ @$unset_var 77); 78 79// loop through each element of $inputs to check the behavior of mb_strrpos() 80$iterator = 1; 81foreach($inputs as $input) { 82 echo "\n-- Iteration $iterator --\n"; 83 var_dump( mb_strrpos($haystack, $needle, $input, $encoding)); 84 $iterator++; 85}; 86 87echo "Done"; 88?> 89--EXPECTF-- 90*** Testing mb_strrpos() : usage variations *** 91 92-- Iteration 1 -- 93int(8) 94 95-- Iteration 2 -- 96int(8) 97 98-- Iteration 3 -- 99 100Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d 101bool(false) 102 103-- Iteration 4 -- 104 105Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d 106bool(false) 107 108-- Iteration 5 -- 109bool(false) 110 111-- Iteration 6 -- 112bool(false) 113 114-- Iteration 7 -- 115 116Warning: mb_strrpos(): Offset is greater than the length of haystack string in %s on line %d 117bool(false) 118 119-- Iteration 8 -- 120int(8) 121 122-- Iteration 9 -- 123int(8) 124 125-- Iteration 10 -- 126int(8) 127 128-- Iteration 11 -- 129int(8) 130 131-- Iteration 12 -- 132int(8) 133 134-- Iteration 13 -- 135int(8) 136 137-- Iteration 14 -- 138int(8) 139 140-- Iteration 15 -- 141int(8) 142 143-- Iteration 16 -- 144 145Notice: Object of class classA could not be converted to int in %s on line %d 146int(8) 147 148-- Iteration 17 -- 149int(8) 150 151-- Iteration 18 -- 152int(8) 153Done 154