Add Php Variable To Js

How to add php variable to js?



$baseurl = "";




        url: baseurl+"/example",

        success: function(data) {





Are you serious?

url: '<?= $baseurl ?>' +"/example",

The js is a another js file.

Ok, that makes sense.

  1. global config variable

<script>baseurl = '<?= $baseurl ?>'</script>

<script src="... external file ..."></script>

  1. constructor

var obj = new YourClass({

    baseurl: '<?= $baseurl ?>'



where YourClass is defined in external file.

There are many ways, it depends on your external js file.