3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('json_encode')) { function json_encode($it) { if (!($assoc = is_object($it)) && !is_array($it)) { return _js_enc_value($it); } if ($assoc) { $it = get_object_vars($it); } else { $assoc = $it === array_values($it); } $data = array(); foreach ($it as $k => $v) { $v = _js_enc_value($v); if ($assoc) { $k = (string)$k; if (preg_match('//u', $k)) { $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 is_int($value) || is_float($value): return $value; case is_string($value) && preg_match('//u', $value): return '"' . _js_uc_esc($value) . '"'; default: return 'null'; } } function _js_uc_esc($value) { $pattern = "@[\x08-\x0a\x0c\x0d\\\\\"/]|([^\x08-\x0a\x0c\x0d\x20-\x7f\\\\\"/])++@u"; 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], "\x08\x09\x0a\x0c\x0d\\\"/"); } } } $var = array( array( 'text', 1, 1.1, true, false, null, array(), new stdClass, ), array( '&\'/"' => "\r\n\t", '' => '', ), 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 git.master, git.master_jit, rfc.property-hooks
string(148) "[["text",1,1.1,true,false,null,[],{}],{"&'\/\"":"\r\n\t","":""},["hoge","foo"],{"0":"hoge","1":"foo"},{"0":"hoge","2":"foo"},{"0":"hoge","2":"foo"}]"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
29.09 ms | 401 KiB | 8 Q