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