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