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