3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php date_default_timezone_set('America/Los_Angeles'); class Fruit { public $type = 'Apple', $lastEaten = null; public function __construct() { $this->lastEaten = new DateTime(); } public function jsonSerialize() { return array( 'type' => $this->type, 'lastEaten' => $this->lastEaten->format(DateTime::ISO8601) ); } } echo $s = json_encode(new Fruit()); var_dump(json_decode($s));
Output for 7.1.0
{"type":"Apple","lastEaten":{"date":"2013-08-20 13:51:44.005832","timezone_type":3,"timezone":"America\/Los_Angeles"}}object(stdClass)#1 (2) { ["type"]=> string(5) "Apple" ["lastEaten"]=> object(stdClass)#2 (3) { ["date"]=> string(26) "2013-08-20 13:51:44.005832" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "America/Los_Angeles" } }
Output for 5.4.30 - 5.4.45, 5.5.14 - 7.0.14
{"type":"Apple","lastEaten":{"date":"2013-08-20 13:51:44.000000","timezone_type":3,"timezone":"America\/Los_Angeles"}}object(stdClass)#1 (2) { ["type"]=> string(5) "Apple" ["lastEaten"]=> object(stdClass)#2 (3) { ["date"]=> string(26) "2013-08-20 13:51:44.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "America/Los_Angeles" } }
Output for 5.3.0 - 5.4.29, 5.5.0 - 5.5.13
{"type":"Apple","lastEaten":{"date":"2013-08-20 13:51:44","timezone_type":3,"timezone":"America\/Los_Angeles"}}object(stdClass)#1 (2) { ["type"]=> string(5) "Apple" ["lastEaten"]=> object(stdClass)#2 (3) { ["date"]=> string(19) "2013-08-20 13:51:44" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "America/Los_Angeles" } }
Output for 5.2.0 - 5.2.17
{"type":"Apple","lastEaten":{}}object(stdClass)#1 (2) { ["type"]=> string(5) "Apple" ["lastEaten"]=> object(stdClass)#2 (0) { } }
Output for 5.1.0 - 5.1.6
Fatal error: Call to undefined function json_encode() in /in/Krt84 on line 22
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Call to undefined function date_default_timezone_set() in /in/Krt84 on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Krt84 on line 6
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Krt84 on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Krt84 on line 6
Process exited with code 255.