Return to intval_variation1.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / general_functions |
1.1 misho 1: --TEST-- 2: Test 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: 11: echo "*** Testing intval() : usage variation ***\n"; 12: 13: // Define error handler 14: function 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: } 20: set_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; 27: unset ($unset_var); 28: 29: // define some classes 30: class classWithToString 31: { 32: public function __toString() { 33: return "Class A object"; 34: } 35: } 36: 37: class classWithoutToString 38: { 39: } 40: 41: // heredoc string 42: $heredoc = <<<EOT 43: hello world 44: EOT; 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: 105: foreach($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-- 116: int(0) 117: 118: --int 1-- 119: int(1) 120: 121: --int 12345-- 122: int(12345) 123: 124: --int -12345-- 125: int(-2345) 126: 127: --float 10.5-- 128: int(10) 129: 130: --float -10.5-- 131: int(-10) 132: 133: --float 12.3456789e5-- 134: int(1234567) 135: 136: --float -12.3456789e5-- 137: int(-1234567) 138: 139: --float .5-- 140: int(0) 141: 142: --empty array-- 143: int(0) 144: 145: --int indexed array-- 146: int(1) 147: 148: --associative array-- 149: int(1) 150: 151: --nested arrays-- 152: int(1) 153: 154: --uppercase NULL-- 155: int(0) 156: 157: --lowercase null-- 158: int(0) 159: 160: --lowercase true-- 161: int(1) 162: 163: --lowercase false-- 164: int(0) 165: 166: --uppercase TRUE-- 167: int(1) 168: 169: --uppercase FALSE-- 170: int(0) 171: 172: --empty string DQ-- 173: int(0) 174: 175: --empty string SQ-- 176: int(0) 177: 178: --string DQ-- 179: int(0) 180: 181: --string SQ-- 182: int(0) 183: 184: --mixed case string-- 185: int(0) 186: 187: --heredoc-- 188: int(0) 189: 190: --instance of classWithToString-- 191: Error: 8 - Object of class classWithToString could not be converted to int, %s(%d) 192: int(1) 193: 194: --instance of classWithoutToString-- 195: Error: 8 - Object of class classWithoutToString could not be converted to int, %s(%d) 196: int(1) 197: 198: --undefined var-- 199: int(0) 200: 201: --unset var-- 202: int(0) 203: ===DONE===