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