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$base = 10; 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 // int data 54 'int 0' => 0, 55 'int 1' => 1, 56 'int 12345' => 12345, 57 'int -12345' => -2345, 58 59 // float data 60 'float 10.5' => 10.5, 61 'float -10.5' => -10.5, 62 'float 12.3456789e5' => 12.3456789e5, 63 'float -12.3456789e5' => -12.3456789e5, 64 'float .5' => .5, 65 66 // array data 67 'empty array' => array(), 68 'int indexed array' => $index_array, 69 'associative array' => $assoc_array, 70 'nested arrays' => array('foo', $index_array, $assoc_array), 71 72 // null data 73 'uppercase NULL' => NULL, 74 'lowercase null' => null, 75 76 // boolean data 77 'lowercase true' => true, 78 'lowercase false' =>false, 79 'uppercase TRUE' =>TRUE, 80 'uppercase FALSE' =>FALSE, 81 82 // empty data 83 'empty string DQ' => "", 84 'empty string SQ' => '', 85 86 // string data 87 'string DQ' => "string", 88 'string SQ' => 'string', 89 'mixed case string' => "sTrInG", 90 'heredoc' => $heredoc, 91 92 // object data 93 'instance of classWithToString' => new classWithToString(), 94 'instance of classWithoutToString' => new classWithoutToString(), 95 96 // undefined data 97 'undefined var' => @$undefined_var, 98 99 // unset data 100 'unset var' => @$unset_var, 101); 102 103// loop through each element of the array for var 104 105foreach($inputs as $key =>$value) { 106 echo "\n--$key--\n"; 107 var_dump( intval($value, $base) ); 108}; 109 110?> 111===DONE=== 112--EXPECTF-- 113*** Testing intval() : usage variation *** 114 115--int 0-- 116int(0) 117 118--int 1-- 119int(1) 120 121--int 12345-- 122int(12345) 123 124--int -12345-- 125int(-2345) 126 127--float 10.5-- 128int(10) 129 130--float -10.5-- 131int(-10) 132 133--float 12.3456789e5-- 134int(1234567) 135 136--float -12.3456789e5-- 137int(-1234567) 138 139--float .5-- 140int(0) 141 142--empty array-- 143int(0) 144 145--int indexed array-- 146int(1) 147 148--associative array-- 149int(1) 150 151--nested arrays-- 152int(1) 153 154--uppercase NULL-- 155int(0) 156 157--lowercase null-- 158int(0) 159 160--lowercase true-- 161int(1) 162 163--lowercase false-- 164int(0) 165 166--uppercase TRUE-- 167int(1) 168 169--uppercase FALSE-- 170int(0) 171 172--empty string DQ-- 173int(0) 174 175--empty string SQ-- 176int(0) 177 178--string DQ-- 179int(0) 180 181--string SQ-- 182int(0) 183 184--mixed case string-- 185int(0) 186 187--heredoc-- 188int(0) 189 190--instance of classWithToString-- 191Error: 8 - Object of class classWithToString could not be converted to int, %s(%d) 192int(1) 193 194--instance of classWithoutToString-- 195Error: 8 - Object of class classWithoutToString could not be converted to int, %s(%d) 196int(1) 197 198--undefined var-- 199int(0) 200 201--unset var-- 202int(0) 203===DONE=== 204