At first it is good to clarify that "Concatenate PHP in JS" is perhaps not the best description of what happens there, I believe that the most correct term would be something like "Dynamically generate JS with PHP", after all what happens is that you create parts of javascript printing (echo) PHP content.
To answer the question: depending on your case I would say that is the most appropriate option yes. If you have a small JS, let’s say something like your example of about 20 lines or less, and it needs to manipulate data that is on the server-side (php), echo and take the content that you need from PHP is simply the simplest way, if it works and it’s readable code there’s no reason why anyone should complain.
If your JS has a reasonable size the situation already changes, having a mega block of javascript in the middle of PHP code is something that I consider appalling, it completely contradicts the code around, it is difficult to read and it is something that clearly breaks the separation of responsibilities. In this case I recommend putting the JS in place, a file. js separate, and for you to have access to the phpnian values (heh that unfortunate word) that you need there are three options:
- Ajax, your javascript asks the server for the values you need. It’s a "pure" solution since you don’t mix php and javascript directly, but it’s the most swollen since you’ll have to have an AJAX block in js, a php endpoint ("url that ajax will call") and an extra request that the client will have to do after loading the page.
- In php echo the data js needs as value of a Hidden input, e.g.:
echo '<input id="mypreciousdata" type="hidden" value="' . $var . '" />';
and in js you do something like console.log($("#mypreciousdata").val());
. This is also a "pure" solution in terms of code separation, does not generate overhead like ajax and is reasonably simple.
- In php you generate a JS that has the data stored in some global object, e.g.:
echo '<script>var MeuApp = {data1: "' . $var . '", data2: "' . $var2 . '"};</script>
and in your JS just access to such variable console.log(MeuApp.data1);
. This is usually my most used strategy because I consider pragmatic. Although mixing js and php code to insert the data into the object will be only a few lines of code in it, being the bulk of your js that will use the data in a separate file.
Actually this is concatenating Javascript in PHP ;)
– bfavaretto
Roughly speaking, it is concatenating a text into another text because javascript does not see php and vice versa. Otherwise, there is no problem in doing this. just recommend that you follow a syntax pattern. For example, there are spacing in the variable dollar and other places without need.
– Daniel Omine
Did any of the answers solve the problem? Do you think you can accept one of them? See [tour] how to do this. You’d be helping the community by identifying the best solution. You can only accept one of them, but you can vote for anything on the entire site.
– Maniero