3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php if (!function_exists('json_encode')) { function json_encode($it) { if (!($assoc = is_object($it)) && !is_array($it)) { return _js_enc_value($it); } $i = 0; $it = (array)$it; if (!$assoc) { foreach ($it as $k => $v) { if ($k !== $i) { $assoc = true; break; } $i++; } } $data = array(); foreach ($it as $k => $v) { $v = _js_enc_value($v); if ($assoc) { $k = _js_enc_value($k); $data[] = $k . ':' . $v; } else { $data[] = $v; } } if ($assoc) { $bl = '{'; $br = '}'; } else { $bl = '['; $br = ']'; } return $bl . implode(',', $data) . $br; } function _js_enc_value($value) { switch (true) { case is_array($value): case is_object($value): return json_encode($value); case is_bool($value): return $value ? 'true' : 'false'; case $value === null: case is_resource($value) && _js_error(): return 'null'; case is_string($value): return '"' . _js_uc_esc($value) . '"'; default: return $value; } } function _js_error() { return trigger_error( 'json_encode(): type is unsupported, encoded as null', E_USER_WARNING ); } function _js_uc_esc($value) { $pattern = "@[\r\n\"'/]|([^\x09\x0A\x0D\x20-\x7E]{3})+@"; return preg_replace_callback($pattern, '_js_uc_esc_cb', $value); } function _js_uc_esc_cb($matches) { switch (true) { case isset($matches[1]): $char = mb_convert_encoding($matches[0], 'UTF-16', 'UTF-8'); $str = ''; $len = strlen($char); for ($i = 0; $i < $len; $i += 2) { $str .= sprintf('\\u%02x%02x', ord($char[$i]), ord($char[$i + 1]) ); } return $str; default: return addcslashes($matches[0], "\r\n\"'/"); } } } $var = array( array( 'text', 1, 1.1, true, false, null, array(), new stdClass, @fopen('php://stdin', 'r'), ), 'あ' => "\\/\"'&#\r\n\s\t\a\v\e\z", array( '0' => 'hoge', '1' => 'foo', ), (object)array( '0' => 'hoge', '1' => 'foo', ), array( '0' => 'hoge', '2' => 'foo', ), (object)array( '0' => 'hoge', '2' => 'foo', ), ); var_dump(json_encode($var));
Output for 5.5.0 - 7.1.0
bool(false)
Output for 5.4.0 - 5.4.45
Warning: json_encode(): type is unsupported, encoded as null in /in/cbP3H on line 120 string(195) "{"0":["text",1,1.1,true,false,null,[],{},null],"\u3042":"\\\/\"'&#\r\n\\s\t\\a\u000b\u001b\\z","1":["hoge","foo"],"2":{"0":"hoge","1":"foo"},"3":{"0":"hoge","2":"foo"},"4":{"0":"hoge","2":"foo"}}"
Output for 5.3.3 - 5.3.29
Warning: json_encode(): type is unsupported, encoded as null in /in/cbP3H on line 120 string(192) "{"0":["text",1,1.1,true,false,null,[],{},null],"\u3042":"\\\/\"'&#\r\n\\s\t\\a\u000b\\e\\z","1":["hoge","foo"],"2":{"0":"hoge","1":"foo"},"3":{"0":"hoge","2":"foo"},"4":{"0":"hoge","2":"foo"}}"
Output for 5.2.10 - 5.3.2
Warning: [json] (php_json_encode) type is unsupported, encoded as null in /in/cbP3H on line 120 string(192) "{"0":["text",1,1.1,true,false,null,[],{},null],"\u3042":"\\\/\"'&#\r\n\\s\t\\a\u000b\\e\\z","1":["hoge","foo"],"2":{"0":"hoge","1":"foo"},"3":{"0":"hoge","2":"foo"},"4":{"0":"hoge","2":"foo"}}"
Output for 5.2.5 - 5.2.9
Warning: [json] (json_encode_r) type is unsupported, encoded as null. in /in/cbP3H on line 120 string(192) "{"0":["text",1,1.1,true,false,null,[],{},null],"\u3042":"\\\/\"'&#\r\n\\s\t\\a\u000b\\e\\z","1":["hoge","foo"],"2":{"0":"hoge","1":"foo"},"3":{"0":"hoge","2":"foo"},"4":{"0":"hoge","2":"foo"}}"
Output for 5.2.0 - 5.2.4
Warning: [json] (json_encode_r) type is unsupported, encoded as null. in /in/cbP3H on line 120 string(189) "{"0":["text",1,1.1,true,false,null,[],{},null],"\u3042":"\\\/\"'&#\r\n\\s\t\\a\\v\\e\\z","1":["hoge","foo"],"2":{"0":"hoge","1":"foo"},"3":{"0":"hoge","2":"foo"},"4":{"0":"hoge","2":"foo"}}"
Output for 4.3.0 - 5.1.6
Warning: json_encode(): type is unsupported, encoded as null in /in/cbP3H on line 61 string(161) "{0:["text",1,1.1,true,false,null,[],{},null],"\u3042":"\\/\"\'&#\r\n\s \a\v\e\z",1:["hoge","foo"],2:{0:"hoge",1:"foo"},3:{0:"hoge",2:"foo"},4:{0:"hoge",2:"foo"}}"