Php value in javascript?

Asked

Viewed 80 times

0

The idea is this below, but it is not working when I put the value of the php variable in javascript, for example-3,3462, -60,6790 It works, you know what it could be?

<?php 
$variavel = "-3,3462, -60,6790";
?>

<script> 

var testMarker = L.marker([<?php $variavel;?>],{icon: orangeIcon});  

</script>

2 answers

4


Change: <?php $variavel;?>

To: <?php echo $variavel;?>

<?php 
$variavel = "-3,3462, -60,6790";
?>

<script> 

var testMarker = L.marker([<?php echo $variavel;?>],{icon: orangeIcon});  

</script>

  • 1

    You can also use <?= $variavel ?> as an alternative to echo.

0

If your variable is an array, do so:

<?php 
  $variavel = ["-3,3462", "-60,6790"];
?>

<script> 

var testMarker = L.marker([<?php echo $variavel[0];?>,<?php echo $variavel[1];?>],{icon: orangeIcon});  
</script>
  • 1

    Apparently it’s just a string. You can also use <?= $variavel ?> as an alternative to echo.

  • 1

    But if there is the possibility of being an array, you can do <?= json_encode($variavel) ?> that the exit will be [..., ..., ...].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.