1--TEST-- 2Test str_pad() function : usage variations - unexpected inputs for '$pad_length' argument 3--FILE-- 4<?php 5/* Prototype : string str_pad ( string $input , int $pad_length [, string $pad_string [, int $pad_type ]] ) 6 * Description: Pad a string to a certain length with another string 7 * Source code: ext/standard/string.c 8*/ 9 10/* Test str_pad() function: with unexpected inputs for '$pad_length' 11 * and expected type for '$input' 12*/ 13 14echo "*** Testing str_pad() function: with unexpected inputs for 'pad_length' argument ***\n"; 15 16//get an unset variable 17$unset_var = 'string_val'; 18unset($unset_var); 19 20//defining a class 21class sample { 22 public function __toString() { 23 return "sample object"; 24 } 25} 26 27//getting the resource 28$file_handle = fopen(__FILE__, "r"); 29 30// array with different values for $input 31$pad_lengths = array ( 32 33 // integer values 34/*1*/ 0, 35 1, 36 -2, 37 255, 38 39 // float values 40/*5*/ 10.5, 41 -20.5, 42 10.12345e2, 43 44 // array values 45/*8*/ array(), 46 array(0), 47 array(1, 2), 48 49 // boolean values 50/*11*/ true, 51 false, 52 TRUE, 53 FALSE, 54 55 // null vlaues 56/*15*/ NULL, 57 null, 58 59 // objects 60/*17*/ new sample(), 61 62 // resource 63/*18*/ $file_handle, 64 65 // undefined variable 66/*19*/ @$undefined_var, 67 68 // unset variable 69/*20*/ @$unset_var 70); 71 72//defining '$input' argument 73$input = "Test string"; 74 75// loop through with each element of the $pad_lengths array to test str_pad() function 76$count = 1; 77foreach($pad_lengths as $pad_length) { 78 echo "-- Iteration $count --\n"; 79 var_dump( str_pad($input, $pad_length) ); 80 $count ++; 81} 82 83fclose($file_handle); //closing the file handle 84 85?> 86===DONE=== 87--EXPECTF-- 88*** Testing str_pad() function: with unexpected inputs for 'pad_length' argument *** 89-- Iteration 1 -- 90string(11) "Test string" 91-- Iteration 2 -- 92string(11) "Test string" 93-- Iteration 3 -- 94string(11) "Test string" 95-- Iteration 4 -- 96string(255) "Test string " 97-- Iteration 5 -- 98string(11) "Test string" 99-- Iteration 6 -- 100string(11) "Test string" 101-- Iteration 7 -- 102string(1012) "Test string " 103-- Iteration 8 -- 104 105Warning: str_pad() expects parameter 2 to be integer, array given in %s on line %d 106NULL 107-- Iteration 9 -- 108 109Warning: str_pad() expects parameter 2 to be integer, array given in %s on line %d 110NULL 111-- Iteration 10 -- 112 113Warning: str_pad() expects parameter 2 to be integer, array given in %s on line %d 114NULL 115-- Iteration 11 -- 116string(11) "Test string" 117-- Iteration 12 -- 118string(11) "Test string" 119-- Iteration 13 -- 120string(11) "Test string" 121-- Iteration 14 -- 122string(11) "Test string" 123-- Iteration 15 -- 124string(11) "Test string" 125-- Iteration 16 -- 126string(11) "Test string" 127-- Iteration 17 -- 128 129Warning: str_pad() expects parameter 2 to be integer, object given in %s on line %d 130NULL 131-- Iteration 18 -- 132 133Warning: str_pad() expects parameter 2 to be integer, resource given in %s on line %d 134NULL 135-- Iteration 19 -- 136string(11) "Test string" 137-- Iteration 20 -- 138string(11) "Test string" 139===DONE=== 140