Loading PHP into JS is a good practice? [duplicate]

3

I see quite often in some questions here from SOpt some codes that can PHP variables within scripts in JS.

Let's assume that the file below is index.php .

<?php
     $var = "Me mostre";
?>

<script>
   function mostrarPalavra(){
      console.log(<?=$var?>);
   }
</script>

This is basic. But it has a recent question that the user is trying to assemble a graph in JS using PHP to bring the information from the database. I suggested to him to do with AJAX and do everything in JS.

Developers and Developers:

Is this a good practice? Or is it best to avoid such situations?

For me this is just an RTA.

Languages support doing something without having to do this Mix.

Although I have specifically referred to PHP and JS, it may be any other languages.

    
asked by anonymous 22.01.2016 / 14:05

1 answer

6

Initial Settings

Good practices

Good practices are bad. Doing right for each situation is what needs to be done.

There are cases that one option is better than the other. Just looking at the concrete case to say. Anyone who says it's good to do this or that without looking at the concrete case is speculating and even doing a disservice.

Gambiarra

Gambiarra is doing what goes against what the requirements ask for. Of course we are talking about realistic requirements and not invented.

Any language

Opening to any language becomes more widespread. It is harder to say that one is better than another. Obviously the AJAX solution only works with JavaScript. And this type of solution is only common using web.

If it is to use this technique with SQL there is already a greater chance of being valid.

With HTML the default is to do this.

Separation of responsibilities

In fact, the ideal is that JS is purer, preferably not go through the generation of PHP, that is, be in separate file and be static. It is a bad practice to mix data with code. JS is essentially code. But it's only really bad if you have another better way to do this . AJAX is not a universal solution. Just because it exists does not mean it should always be used.

Examples of the SOpt

Of course, the questions in SOpt and other sites are not usually good references for not doing something. There is a minority of cases that can be used as a good reference.

Is this correct?

If it's okay to do this, it's okay to do . Provided you have a justification to do so. The problem is to do it because everyone is doing it. It is a problem to take the dessert recipe full of sugar and serve in a meeting of diabetics. But what's the problem of making delicious sugary desserts at a confectioners meeting? You have to understand the motivations, you have to know why you are doing this. Everything is valid and correct.

Placing the result of a code in PHP as part of what will be inserted into the JS code that will be sent to the browser is absolutely normal. It should only be used if it has no sense in doing. I understand that the example of the question is illustrative only, but it is a case where there is no need to do this.

Poorly typed JavaScript

The biggest problem of the question code mentioned (in the comments) is not PHP, it is the JS that is not parameterized. You are using JS as a code template and not as a normal function. If JS is well written and has a function that receives parameters, if you send the data through HTML and just call the JS function, it really is a better way. Isolates the JS, code, HTML, data.

In fact the example quoted would be better written otherwise.

AJAX

AJAX is just one solution for sending data out of HTML as well. It really can be a good option in some cases. But note that if JS is right, it makes little difference if the data came from the HTML or AJAX request. IF AJAX can not or should not be used in the concrete case, it is not a solution.

Conclusion

Overall I would not do this in most situations I encountered. I would separate the HTML from the JS well. But I would never say that it is wrong to do in any case.

Bacco gave an example in the comments. I think instead of by in HTML some data, can put in an auxiliary JS and call the parameterized JS. Let's say this would be an AJAX without being AJAX :) It might be useful not to pollute HTML with data that is not needed on it. But note that there will still be a clear separation of responsibilities between what is given in JS and what is code.

    
22.01.2016 / 14:53