3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('json_encode')) { function json_encode($it) { if (!is_array($it) && !is_object($it)) { return _js_enc_value($it); } $assoc = false; $i = 0; foreach ($it as $k => $v) { if ($k !== $i) { $assoc = true; break; } $i++; } $data = array(); foreach ($it as $k => $v) { $k = '"'. addcslashes($k, '"\\') . '"'; $v = _js_enc_value($v); $data[] = $assoc ? $key . ':' . $value : $value; } if ($assoc) { $spl = '{'; $spr = '}'; } else { $spl = '['; $spr = ']'; } return $spl . implode(',', $data) . $spr; } 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): return 'null'; case is_string($value): return '"' . _js_uni_esc($value) . '"'; default: return $value; } } function _js_uni_esc($value) { $pattern = "/\r|(\n)|((?:[^\x09\x0A\x0D\x20-\x7E]{3})+)/"; return preg_replace_callback($pattern, '_js_uni_esc_cb', $value); } function _js_uni_esc_cb($matches) { switch (true) { case isset($matches[2]): $char = mb_convert_encoding($matches[2], '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; case isset($matches[1]): return '\\n'; default: return '\\r'; } } } $var = array( 0 => array('text', 1, 1.1, true, false, null, stdClass, STDIN), 2 => array('a' => 'hoge', 'b' => 'foo'), ); echo json_encode($var);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Undefined constant "stdClass" in /in/aOUca:76 Stack trace: #0 {main} thrown in /in/aOUca on line 76
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33
Warning: Use of undefined constant stdClass - assumed 'stdClass' (this will throw an Error in a future version of PHP) in /in/aOUca on line 76
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76
Output for 5.3.3 - 5.3.29, 5.4.0 - 5.4.45
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76 Warning: json_encode(): type is unsupported, encoded as null in /in/aOUca on line 80 {"0":["text",1,1.1,true,false,null,"stdClass",null],"2":{"a":"hoge","b":"foo"}}
Output for 5.2.10 - 5.2.17, 5.3.0 - 5.3.2
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76 Warning: [json] (php_json_encode) type is unsupported, encoded as null in /in/aOUca on line 80 {"0":["text",1,1.1,true,false,null,"stdClass",null],"2":{"a":"hoge","b":"foo"}}
Output for 5.2.3 - 5.2.9
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76 Warning: [json] (json_encode_r) type is unsupported, encoded as null. in /in/aOUca on line 80 {"0":["text",1,1.1,true,false,null,"stdClass",null],"2":{"a":"hoge","b":"foo"}}
Output for 5.2.0 - 5.2.2
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76 Notice: Use of undefined constant STDIN - assumed 'STDIN' in /in/aOUca on line 76 {"0":["text",1,1.1,true,false,null,"stdClass","STDIN"],"2":{"a":"hoge","b":"foo"}}
Output for 5.0.4 - 5.0.5, 5.1.0 - 5.1.6
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76 Notice: Use of undefined constant STDIN - assumed 'STDIN' in /in/aOUca on line 76 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 {:,:}
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.3
Notice: Use of undefined constant stdClass - assumed 'stdClass' in /in/aOUca on line 76 Notice: Use of undefined constant STDIN - assumed 'STDIN' in /in/aOUca on line 76 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 Notice: Undefined variable: key in /in/aOUca on line 22 Notice: Undefined variable: value in /in/aOUca on line 22 {:,:}

preferences:
213.86 ms | 404 KiB | 354 Q