1--TEST-- 2Test str_pad() function : usage variations - unexpected inputs for '$pad_string' 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_string' 11 * and expected type for '$input' and '$pad_length' 12*/ 13 14echo "*** Testing str_pad() function: with unexpected inputs for 'pad_string' 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_strings = array ( 32 33 // integer values 34/*1*/ 0, 35 1, 36 -2, 37 2147483647, 38 -2147483648, 39 40 // float values 41/*6*/ 10.5, 42 -20.5, 43 10.1234567e10, 44 45 // array values 46/*9*/ array(), 47 array(0), 48 array(1, 2), 49 50 // boolean values 51/*12*/ true, 52 false, 53 TRUE, 54 FALSE, 55 56 // null values 57/*16*/ NULL, 58 null, 59 60 // objects 61/*18*/ new sample(), 62 63 // resource 64/*19*/ $file_handle, 65 66 // undefined variable 67/*20*/ @$undefined_var, 68 69 // unset variable 70/*21*/ @$unset_var 71); 72 73//defining '$input' argument 74$input = "Test string"; 75$pad_length = 20; 76 77// loop through with each element of the $pad_strings array to test str_pad() function 78$count = 1; 79foreach($pad_strings as $pad_string) { 80 echo "-- Iteration $count --\n"; 81 var_dump( str_pad($input, $pad_length, $pad_string) ); 82 $count ++; 83} 84 85fclose($file_handle); //closing the file handle 86 87?> 88===DONE=== 89--EXPECTF-- 90*** Testing str_pad() function: with unexpected inputs for 'pad_string' argument *** 91-- Iteration 1 -- 92string(20) "Test string000000000" 93-- Iteration 2 -- 94string(20) "Test string111111111" 95-- Iteration 3 -- 96string(20) "Test string-2-2-2-2-" 97-- Iteration 4 -- 98string(20) "Test string214748364" 99-- Iteration 5 -- 100string(20) "Test string-21474836" 101-- Iteration 6 -- 102string(20) "Test string10.510.51" 103-- Iteration 7 -- 104string(20) "Test string-20.5-20." 105-- Iteration 8 -- 106string(20) "Test string101234567" 107-- Iteration 9 -- 108 109Warning: str_pad() expects parameter 3 to be string, array given in %s on line %d 110NULL 111-- Iteration 10 -- 112 113Warning: str_pad() expects parameter 3 to be string, array given in %s on line %d 114NULL 115-- Iteration 11 -- 116 117Warning: str_pad() expects parameter 3 to be string, array given in %s on line %d 118NULL 119-- Iteration 12 -- 120string(20) "Test string111111111" 121-- Iteration 13 -- 122 123Warning: str_pad(): Padding string cannot be empty in %s on line %d 124NULL 125-- Iteration 14 -- 126string(20) "Test string111111111" 127-- Iteration 15 -- 128 129Warning: str_pad(): Padding string cannot be empty in %s on line %d 130NULL 131-- Iteration 16 -- 132 133Warning: str_pad(): Padding string cannot be empty in %s on line %d 134NULL 135-- Iteration 17 -- 136 137Warning: str_pad(): Padding string cannot be empty in %s on line %d 138NULL 139-- Iteration 18 -- 140string(20) "Test stringsample ob" 141-- Iteration 19 -- 142 143Warning: str_pad() expects parameter 3 to be string, resource given in %s on line %d 144NULL 145-- Iteration 20 -- 146 147Warning: str_pad(): Padding string cannot be empty in %s on line %d 148NULL 149-- Iteration 21 -- 150 151Warning: str_pad(): Padding string cannot be empty in %s on line %d 152NULL 153===DONE=== 154