3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class myClass { public $item1 = 1; public $item2 = '中文'; function to_json() { //url编码,避免json_encode将中文转为unicode $this->item2 = urlencode($this->item2); $str_json = json_encode($this); //url解码,转完json后将各属性返回,确保对象属性不变 $this->item2 = urldecode($this->item2); return urldecode($str_json); } } $c = new myClass(); echo json_encode($c); echo '<br/>'; echo $c->to_json(); echo '<br/>'; echo json_encode($c); echo '<br/>'; echo json_encode('胥');
Output for 5.2.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
{"item1":1,"item2":"\u4e2d\u6587"}<br/>{"item1":1,"item2":"中文"}<br/>{"item1":1,"item2":"\u4e2d\u6587"}<br/>"\u80e5"
Output for 5.0.0 - 5.1.6
Fatal error: Call to undefined function json_encode() in /in/aW0f5 on line 17
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/aW0f5 on line 3
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/aW0f5 on line 3
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/aW0f5 on line 3
Process exited with code 255.