I have a php file (test.php
) that has the following content:
<?php
$ar = array('apple', 'orange', 'banana', 'strawberry');
?>
When I now want to use the $ar
variable in a javascript code inside my test.html
file (which looks as follows):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF8">
<script src="test.php" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var ar = <?php echo json_encode($ar) ?>;
alert(ar)
</script>
</body>
</html>
I keep getting the error: SyntaxError: Unexpected token '<'
(in Chrome and Safari). I read through many other examples, where this error was mentioned, however, none of the proposed solutions worked for me. Could anyone help me here?