26

Yesterday I was told to not mix HTML with PHP.

My reaction:
Why not? And how else should I do?

Don't get a answer to this.

Comments
  • 7
    IMO, PHP is a pretty good templating language in and of itself. I guess you could use something like twig or nunjucks (for the more JavaScript inclined) but if your pages are rendered server side then plain PHP is good enough.
  • 5
    echo '<script>
    var para = document.createElement("p");
    var node = document.createTextNode("This is new.");
    para.appendChild(node);

    var element = document.getElementById("div1");
    element.appendChild(para);
    </script>';

    easy 😕
  • 4
    Generally it's a good idea to separate the pieces that do different things. This way it's easier to decipher what's what and the code becomes more manageable. Especially if you're not the only one working on the code.
    You still may need to inject some variables into your html tags which is fine. But if you have php functions and logic that can be written elsewhere then you should do so.
  • 6
    MVC...
    Make them in php but dont put the logic in there.

    Dont show html and do mysql query in same file.
  • 7
    Lifehack, you cant mix html with php if you dont use php.
  • 1
    Angular or react
  • 0
    @curlyDev MVC doesn't apply much in the server side it is pretty linear, it is just separation of concerns
  • 1
    What they usually mean, is that in your views, you shouldn't have more than if's and for loops.

    No further logic or calculations or database interaction or whatever.
  • 0
    My solution is to only have php: The html parts are all in heredocs and get assembled into one variable that is echoed at the end. Pure php, just what you wanted :p
  • 2
    Build a JSON api using php

    Use html and js to consume it.
    In short you don't mix html with php because php generates json which is basically arrays and js reads them. You use ajax to send and receive data from php.
    That's how I began...
Add Comment