Is it possible to use in javascript a variable that was defined in earlier PHP code?
For example (in a page template PHP file):
<?php
$arr = array(-34, 150);
?>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
...
var latlng = new google.maps.LatLng($arr);
...
}
</script>
Even better, use wp_localize_script() to pass your variables from PHP to javascript:
wp_enqueue_script( 'my-script', '/path/to/my-script.js' );
$loc_variables = array(
'lat' => $latitude,
'lon' => $longitude
);
wp_localize_script( 'my-script', 'location', $loc_variables );
And then in your my-script.js, you can access those variables as location.lat and location.lon.
No, but you can make it a js var...
<script type="text/javascript">
var myArray = new Array(<?php echo $myArrayVals; ?>);
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With