I have made some silly error, but can not work out what I have done.
I am attempting to test passing variables from PHP to Javascript and if it is an array, json_encode
it
My file is a PHP file ie .php
The php line of code that seems to be causing the error I have added to the original PHP and it works OK
<?php
$php_var = 'lol';
$php_array = array ();
$php_array["lady"] = "mary";
$php_array["gent"] = "joseph";
echo is_array($php_array) ? json_encode($php_array) : $php_array; // same as faulty line in javascript
?>
<html>
<body>
<script type="text/javascript" charset="utf-8">
var php_var = "<?php if (is_array($php_var)) {echo json_encode($php_var); } else { echo $php_var;}; ?>";
document.write(php_var + ' ifElse<br>');
// THE FOLLOWING LINE GIVES Uncaught SyntaxError: Unexpected identifier
var php_var2 = "<?php echo is_array($php_array) ? json_encode($php_array) : $php_array; ?>";
document.write (php_var2 + ' EitherOR<br>');
alert(php_var + php_array);
</script>
<h1> Testing Jscript variables</h1>
</body>
</html>