1--TEST-- 2Test json_encode() function : basic functionality 3--SKIPIF-- 4<?php 5if (!extension_loaded("json")) { 6 die('skip JSON extension not available in this build'); 7} 8?> 9--FILE-- 10<?php 11/* Prototype : string json_encode ( mixed $value ) 12 * Description: Returns the JSON representation of a value 13 * Source code: ext/json/php_json.c 14 * Alias to functions: 15 */ 16echo "*** Testing json_encode() : basic functionality ***\n"; 17 18//get an unset variable 19$unset_var = 10; 20unset ($unset_var); 21 22// get a resource variable 23$fp = fopen(__FILE__, "r"); 24 25// get an object 26class sample { 27} 28 29$obj = new sample(); 30$obj->MyInt = 99; 31$obj->MyFloat = 123.45; 32$obj->MyBool = true; 33$obj->MyNull = null; 34$obj->MyString = "Hello World"; 35 36// array with different values for $string 37$inputs = array ( 38 39 // integers 40/*1*/ 0, 41 123, 42 -123, 43 2147483647, 44 -2147483648, 45 46 // floats 47/*6*/ 123.456, 48 1.23E3, 49 -1.23E3, 50 51 // boolean 52/*9*/ TRUE, 53 true, 54 FALSE, 55 false, 56 57 // NULL 58/*13*/ NULL, 59 null, 60 61 // strings 62/*15*/ "abc", 63 'abc', 64 "Hello\t\tWorld\n", 65 66 // arrays 67/*18*/ array(), 68 array(1,2,3,4,5), 69 array(1 => "Sun", 2=>"Mon", 3 => "Tue", 4 => "Wed", 5 => "Thur", 6 => "Fri", 7 => "Sat"), 70 array("Jan" => 31, "Feb" => 29, "Mar" => 31, "April" => 30, "May" => 31, "June" => 30), 71 72 // empty data 73/*22*/ "", 74 '', 75 76 // undefined data 77/*24*/ @$undefined_var, 78 79 // unset data 80/*25*/ @$unset_var, 81 82 // resource variable 83/*26*/ $fp, 84 85 // object variable 86/*27*/ $obj 87 88); 89 90// loop through with each element of the $inputs array to test json_encode() function 91$count = 1; 92foreach($inputs as $input) { 93 echo "-- Iteration $count --\n"; 94 var_dump(json_encode($input)); 95 $count ++; 96} 97 98?> 99===Done=== 100--EXPECTF-- 101*** Testing json_encode() : basic functionality *** 102-- Iteration 1 -- 103string(1) "0" 104-- Iteration 2 -- 105string(3) "123" 106-- Iteration 3 -- 107string(4) "-123" 108-- Iteration 4 -- 109string(10) "2147483647" 110-- Iteration 5 -- 111string(11) "-2147483648" 112-- Iteration 6 -- 113string(7) "123.456" 114-- Iteration 7 -- 115string(4) "1230" 116-- Iteration 8 -- 117string(5) "-1230" 118-- Iteration 9 -- 119string(4) "true" 120-- Iteration 10 -- 121string(4) "true" 122-- Iteration 11 -- 123string(5) "false" 124-- Iteration 12 -- 125string(5) "false" 126-- Iteration 13 -- 127string(4) "null" 128-- Iteration 14 -- 129string(4) "null" 130-- Iteration 15 -- 131string(5) ""abc"" 132-- Iteration 16 -- 133string(5) ""abc"" 134-- Iteration 17 -- 135string(18) ""Hello\t\tWorld\n"" 136-- Iteration 18 -- 137string(2) "[]" 138-- Iteration 19 -- 139string(11) "[1,2,3,4,5]" 140-- Iteration 20 -- 141string(72) "{"1":"Sun","2":"Mon","3":"Tue","4":"Wed","5":"Thur","6":"Fri","7":"Sat"}" 142-- Iteration 21 -- 143string(58) "{"Jan":31,"Feb":29,"Mar":31,"April":30,"May":31,"June":30}" 144-- Iteration 22 -- 145string(2) """" 146-- Iteration 23 -- 147string(2) """" 148-- Iteration 24 -- 149string(4) "null" 150-- Iteration 25 -- 151string(4) "null" 152-- Iteration 26 -- 153 154Warning: json_encode(): type is unsupported, encoded as null in %s on line %d 155string(4) "null" 156-- Iteration 27 -- 157string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}" 158===Done===