3v4l.org

run code in 300+ PHP versions simultaneously
<?php // The JSON extension wasn't bundled with PHP until version 5.2.0 if (version_compare(PHP_VERSION, '5.1.6', '<=')) exit('PHP version not supported: json_decode() not available'); // Stop 3v4l.org from showing parse errors in old PHP versions without support for classes or interfaces else {$_ = __COMPILER_HALT_OFFSET__; eval(substr(file_get_contents(__FILE__), $_ + 2));} __halt_compiler();?> // Polyfill JsonSerializable if (!interface_exists('JsonSerializable', FALSE)) { interface JsonSerializable { public function jsonSerialize(); } function json_serialize(JsonSerializable $object) { return json_encode($object->jsonSerialize()); } } interface JsonUnserializable extends JsonSerializable { public function jsonUnserialize($json); } class User implements JsonUnserializable { private $_name; public function __construct($name = NULL) { $this->setName($name); } public function setName($name) { $this->_name = $name; } public function getName() { return $this->_name; } public function jsonSerialize() { return array('name' => $this->getName()); } public function jsonUnserialize($json) { $this->setName($json->name); } } function json_unserialize($json, $className) { if (version_compare(PHP_VERSION, '5.3.9', '>=')) { $isUnserializable = is_a($className, 'JsonUnserializable', TRUE); } else { $isUnserializable = new $className instanceof JsonUnserializable; } if ($isUnserializable) { $object = new $className; $object->jsonUnserialize(json_decode($json)); return $object; } else { return FALSE; } } if (version_compare(PHP_VERSION, '5.4.0', '>=')) { assert(json_encode(new User('Bar')) === '{"name":"Bar"}'); } else { assert(json_serialize(new User('Bar')) === '{"name":"Bar"}'); } $user = json_unserialize('{"name":"Foo"}', 'User'); var_dump($user->getName());
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.21, 8.3.0 - 8.3.9
Deprecated: Return type of User::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/vvv2g(7) : eval()'d code on line 28 string(3) "Foo"
Output for 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
string(3) "Foo"
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
PHP version not supported: json_decode() not available

preferences:
233.14 ms | 403 KiB | 319 Q