1--TEST-- 2Test strtok() function : usage variations - first argument as non-string 3--FILE-- 4<?php 5/* Prototype : string strtok ( string $str, string $token ) 6 * Description: splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token 7 * Source code: ext/standard/string.c 8*/ 9 10/* 11 * Testing strtok() : with first argument as non-string 12*/ 13 14echo "*** Testing strtok() : with first argument as non-string ***\n"; 15// initialize all required variables 16$token = '-'; 17 18// get an unset variable 19$unset_var = 'string_val'; 20unset($unset_var); 21 22// declaring a class 23class sample { 24 public function __toString() { 25 return "obj-ect"; 26 } 27} 28 29// Defining resource 30$file_handle = fopen(__FILE__, 'r'); 31 32// array with different values 33$values = array ( 34 35 // integer values 36 0, 37 1, 38 12345, 39 -2345, 40 41 // float values 42 10.5, 43 -10.5, 44 10.1234567e10, 45 10.7654321E-10, 46 .5, 47 48 // array values 49 array(), 50 array(0), 51 array(1), 52 array(1, 2), 53 array('color' => 'red-color', 'item' => 'pen-color'), 54 55 // boolean values 56 true, 57 false, 58 TRUE, 59 FALSE, 60 61 // objects 62 new sample(), 63 64 // empty string 65 "", 66 '', 67 68 // null vlaues 69 NULL, 70 null, 71 72 // undefined variable 73 $undefined_var, 74 75 // unset variable 76 $unset_var, 77 78 // resource 79 $file_handle 80); 81 82 83// loop through each element of the array and check the working of strtok() 84// when $str arugment is supplied with different values 85 86echo "\n--- Testing strtok() by supplying different values for 'str' argument ---\n"; 87$counter = 1; 88for($index = 0; $index < count($values); $index ++) { 89 echo "-- Iteration $counter --\n"; 90 $str = $values [$index]; 91 92 var_dump( strtok($str, $token) ); 93 94 $counter ++; 95} 96 97//closing the resource 98fclose($file_handle); 99 100echo "Done\n"; 101?> 102--EXPECTF-- 103*** Testing strtok() : with first argument as non-string *** 104 105Notice: Undefined variable: undefined_var in %s on line %d 106 107Notice: Undefined variable: unset_var in %s on line %d 108 109--- Testing strtok() by supplying different values for 'str' argument --- 110-- Iteration 1 -- 111string(1) "0" 112-- Iteration 2 -- 113string(1) "1" 114-- Iteration 3 -- 115string(5) "12345" 116-- Iteration 4 -- 117string(4) "2345" 118-- Iteration 5 -- 119string(4) "10.5" 120-- Iteration 6 -- 121string(4) "10.5" 122-- Iteration 7 -- 123string(12) "101234567000" 124-- Iteration 8 -- 125string(11) "1.07654321E" 126-- Iteration 9 -- 127string(3) "0.5" 128-- Iteration 10 -- 129 130Warning: strtok() expects parameter 1 to be string, array given in %s on line %d 131NULL 132-- Iteration 11 -- 133 134Warning: strtok() expects parameter 1 to be string, array given in %s on line %d 135NULL 136-- Iteration 12 -- 137 138Warning: strtok() expects parameter 1 to be string, array given in %s on line %d 139NULL 140-- Iteration 13 -- 141 142Warning: strtok() expects parameter 1 to be string, array given in %s on line %d 143NULL 144-- Iteration 14 -- 145 146Warning: strtok() expects parameter 1 to be string, array given in %s on line %d 147NULL 148-- Iteration 15 -- 149string(1) "1" 150-- Iteration 16 -- 151bool(false) 152-- Iteration 17 -- 153string(1) "1" 154-- Iteration 18 -- 155bool(false) 156-- Iteration 19 -- 157string(3) "obj" 158-- Iteration 20 -- 159bool(false) 160-- Iteration 21 -- 161bool(false) 162-- Iteration 22 -- 163bool(false) 164-- Iteration 23 -- 165bool(false) 166-- Iteration 24 -- 167bool(false) 168-- Iteration 25 -- 169bool(false) 170-- Iteration 26 -- 171 172Warning: strtok() expects parameter 1 to be string, resource given in %s on line %d 173NULL 174Done 175