Is loading PHP into JS a good practice?

Asked

Viewed 986 times

5

I see quite often in some questions here Sopt some codes that have PHP variables within scripts in JS.

Let’s assume the file below is index.php.

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

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

This is pretty basic. But there is a recent question that the user is trying to assemble a JS chart using PHP to bring the information from the database. I suggested to him to do with AJAX and do everything on JS.

Developers and programmers:

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

NOTE: I remember that when I was starting in this area, when I wanted to solve some things in a system I did this... of putting PHP inside JS.

To me it’s just RTA.

The languages give support to do something without having to do this Mix.

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

  • 2

    Immediately the readability of the code (mainly PHP) is impaired.

  • 1

    What is RTA????

  • 4

    Alternative Technical Resource, alias Gambiarra. Example: http://answall.com/questions/109680/grafico-em-barras-char-js

  • 1

    The questions where you see this are closed? rs

  • Rsrs do not know if I understand your thinking, but usually they are not closed.

  • 7

    For me the ugliest gambiarra there is to use tag script within PHP with console..

  • What is the alternative @Bacco?

  • That example didn’t work.

  • 4

    @durtto log in by PHP itself, show right on screen to anyone who has a developer cookie, show as html comment, access a page from outside to register the error, there are a thousand ways. depends on the case. Nor would it fit in the comment how much alternative you can do. Generally calling <script> PHP is already a code that stinks me.

  • 7

    As for what was asked, I find it too broad, because there are cases and cases. For example, if you are generating a JS map script using PHP to merge coordinates, it is all right. Depending on the case it can be great, depending on the case it can be very bad to merge. If you know what you’re doing, you can merge CSS, JS, SQL and HTML all together in the same PHP that looks beautiful. If you don’t know, it gets disgusting.

  • Got it @Bacco. I read everyone’s opinion, including the answer below. For me you can close.

  • It seems to be duplicate yes. I actually wanted to know if it’s correct. I saw that there is nothing wrong with doing this, however, it would be good to follow the language pattern. I would not do it anymore.

  • @Zoom I closed, but qq thing leave a comment here if you need to review something.

  • If the reply of the bigown served, it would be nice to close the post with an Accept not to be open on the list (soon delete the comment)

  • Did the answer resolve what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

Show 10 more comments

1 answer

9

Initial definitions

Good practice

Good practices are bad. Doing the right thing for every situation is what should be done.

There are cases where one option is better than the other, just analyzing the concrete case to say. Anyone who says it is good to do this or that without looking at the concrete case is speculating and even rendering a disservice.

Gambiarra

Gambiarra is to do what goes against what the requirements require. Of course we are talking about realistic and not invented requirements.

Any language

Opening yourself up to any language is even more widespread. It is more difficult 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 to use this technique with SQL there is already a greater chance of being valid.

With HTML the default is to do just that.

Separation of responsibilities

In fact the ideal is that the JS is purer, preferably not go through the generation of PHP, ie 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 there’s another better way to do it. AJAX is not a universal solution. Just because it exists does not mean it should always be used.

Examples of Sopt

Of course, the existing questions on Sopt and other sites are usually not good references to how not to do something. There are a minority of cases that can be used as a good reference.

It’s okay to do this?

If it’s okay to do this, it’s okay to do it, as long as you have a justification to do it, the problem is to do it because everyone is doing it.

It’s a problem to take a dessert recipe filled with sugar and serve it on a diabetic date. But what’s wrong with making sweet, tasty desserts at a confectioners' meeting?

You have to understand the motivations, you have to know why you are doing it. Then everything is valid and correct.

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

Misspelled Javascript

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

In fact the cited example would be better written in another way.

AJAX

AJAX is just a solution for sending data outside of HTML as well. It might actually be a good option in some cases. Note that if JS is right, it makes little difference whether the data came from HTML or AJAX request. If AJAX cannot or should not be used in the specific case, it is not solution.

Completion

Overall I wouldn’t do this in most situations I encountered. I would well separate the HTML from the JS, but I would never say it is wrong to do in any case.

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

Browser other questions tagged

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