2017-2018 and above solution:
Since no one has raised it yet, and I think no one thought about combining the base64_encode and json_encode , but you can even send PHP Array variables like this:
index.php
<?php $string = "hello"; $array = ['hi', 'how', 'are', 'you']; $array = base64_encode(json_encode($array));
Then you can simply load the desired js file with the query string parameter as follows:
echo '<script type="text/javascript" src="js/main.php?string='.$string.'&array='.$array.'">';
Then js/main.php will look like this, for example. You can check your variables as follows:
Js / main.php
<?php if ($_GET['string']) { $a = $_GET['string']; } if ($_GET['array']) { $b = $_GET['array']; } $b = json_decode(base64_decode($b)); echo 'alert("String $a: + '.$a.'");'; echo 'alert("First key of Array $array: + '.$b[0].'");'; exit(); ?>
When you open index.php following is displayed. So you see, you are not opening js/main.php , and you still have javascript functionality.

AlexioVay Oct 06 '17 at 7:40 2017-10-06 07:40
source share