how to write php in script tag code example
Example 1: how to write php in script file
<script type="text/javascript">
var phpVars = {
<?php
foreach ($phpToJsVars as $key => $value) {
echo ' ' . $key . ': ' . '"' . $value . '",' . "\n";
}
?>
};
</script>
Example 2: how to write php in script file
<?php
$phpToJsVars = [
'value1' => 'foo1',
'value2' => 'foo2'
];
?>
Example 3: how to write php in script file
phpVars["value1"]
phpVars["value2"]