Without using AJAX, how to correctly write a JSON string to JS from PHP. Example below, some case will break this code. I'm trying to find a perfect way to display json correctly, for both case "a" and "b"
Write the output JS to browser console, you'll see it breaks.
<?php
$var = array(
'nokey',
5 => 'xyz',
'key' => '456',
//bug
"apostrophe ' xx" => 'quotes " xx',
'\'' => "\"",
'malicious backslash \ ' => 'double \\',
"line break
"
);
$var = json_encode($var);
?>
<script>
//bug
var a = JSON.parse('<?php echo $var ?>');
var b = JSON.parse("<?php echo $var ?>");
</script>
http://sandbox.onlinephpfunctions.com/code/5c860f978ddd6d196b15c58f55db20de34bcf72c