1--TEST-- 2Test intval() function : usage variation 3--FILE-- 4<?php 5/* Prototype : int intval(mixed var [, int base]) 6 * Description: Get the integer value of a variable using the optional base for the conversion 7 * Source code: ext/standard/type.c 8 * Alias to functions: 9 */ 10 11echo "*** Testing intval() : usage variation ***\n"; 12 13// Define error handler 14function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) { 15 if (error_reporting() != 0) { 16 // report non-silenced errors 17 echo "Error: $err_no - $err_msg, $filename($linenum)\n"; 18 } 19} 20set_error_handler('test_error_handler'); 21 22// Initialise function arguments not being substituted (if any) 23$var = 1; 24 25//get an unset variable 26$unset_var = 10; 27unset ($unset_var); 28 29// define some classes 30class classWithToString 31{ 32 public function __toString() { 33 return "Class A object"; 34 } 35} 36 37class classWithoutToString 38{ 39} 40 41// heredoc string 42$heredoc = <<<EOT 43hello world 44EOT; 45 46// add arrays 47$index_array = array (1, 2, 3); 48$assoc_array = array ('one' => 1, 'two' => 2); 49 50//array of values to iterate over 51$inputs = array( 52 53 // float data 54 'float 10.5' => 10.5, 55 'float -10.5' => -10.5, 56 'float 12.3456789000e10' => 12.3456789000e10, 57 'float -12.3456789000e10' => -12.3456789000e10, 58 'float .5' => .5, 59 60 // array data 61 'empty array' => array(), 62 'int indexed array' => $index_array, 63 'associative array' => $assoc_array, 64 'nested arrays' => array('foo', $index_array, $assoc_array), 65 66 // null data 67 'uppercase NULL' => NULL, 68 'lowercase null' => null, 69 70 // boolean data 71 'lowercase true' => true, 72 'lowercase false' =>false, 73 'uppercase TRUE' =>TRUE, 74 'uppercase FALSE' =>FALSE, 75 76 // empty data 77 'empty string DQ' => "", 78 'empty string SQ' => '', 79 80 // string data 81 'string DQ' => "string", 82 'string SQ' => 'string', 83 'mixed case string' => "sTrInG", 84 'heredoc' => $heredoc, 85 86 // object data 87 'instance of classWithToString' => new classWithToString(), 88 'instance of classWithoutToString' => new classWithoutToString(), 89 90 // undefined data 91 'undefined var' => @$undefined_var, 92 93 // unset data 94 'unset var' => @$unset_var, 95); 96 97// loop through each element of the array for base 98 99foreach($inputs as $key =>$value) { 100 echo "\n--$key--\n"; 101 var_dump( intval($var, $value) ); 102}; 103 104?> 105===DONE=== 106--EXPECTF-- 107*** Testing intval() : usage variation *** 108 109--float 10.5-- 110int(1) 111 112--float -10.5-- 113int(1) 114 115--float 12.3456789000e10-- 116int(1) 117 118--float -12.3456789000e10-- 119int(1) 120 121--float .5-- 122int(1) 123 124--empty array-- 125Error: 2 - intval() expects parameter 2 to be long, array given, %s(%d) 126NULL 127 128--int indexed array-- 129Error: 2 - intval() expects parameter 2 to be long, array given, %s(%d) 130NULL 131 132--associative array-- 133Error: 2 - intval() expects parameter 2 to be long, array given, %s(%d) 134NULL 135 136--nested arrays-- 137Error: 2 - intval() expects parameter 2 to be long, array given, %s(%d) 138NULL 139 140--uppercase NULL-- 141int(1) 142 143--lowercase null-- 144int(1) 145 146--lowercase true-- 147int(1) 148 149--lowercase false-- 150int(1) 151 152--uppercase TRUE-- 153int(1) 154 155--uppercase FALSE-- 156int(1) 157 158--empty string DQ-- 159Error: 2 - intval() expects parameter 2 to be long, string given, %s(%d) 160NULL 161 162--empty string SQ-- 163Error: 2 - intval() expects parameter 2 to be long, string given, %s(%d) 164NULL 165 166--string DQ-- 167Error: 2 - intval() expects parameter 2 to be long, string given, %s(%d) 168NULL 169 170--string SQ-- 171Error: 2 - intval() expects parameter 2 to be long, string given, %s(%d) 172NULL 173 174--mixed case string-- 175Error: 2 - intval() expects parameter 2 to be long, string given, %s(%d) 176NULL 177 178--heredoc-- 179Error: 2 - intval() expects parameter 2 to be long, string given, %s(%d) 180NULL 181 182--instance of classWithToString-- 183Error: 2 - intval() expects parameter 2 to be long, object given, %s(%d) 184NULL 185 186--instance of classWithoutToString-- 187Error: 2 - intval() expects parameter 2 to be long, object given, %s(%d) 188NULL 189 190--undefined var-- 191int(1) 192 193--unset var-- 194int(1) 195===DONE===