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