Why is it not advisable to use PHP codes in the same HTML document?

3

I was in the lab today showing a simple PHP code sample to a colleague and my teacher saw it and asked "Why are you putting PHP together with HTML?". Even without understanding much I replied that it was just a simple example that I had made to show my colleague. He replied that even being an example it was not advisable to do this. I should have asked him why, but I did not ask and I had this little doubt.

Why is it not advisable to use PHP together with HTML? Should I create a specific document just for PHP separate from HTML? How do I call these codes in PHP without having to change the contents of the page?

    
asked by anonymous 05.09.2017 / 02:01

4 answers

4

I'm yours, in simple things, make it as simple as possible.

Of course you are learning the ideal is to learn to do the same simple examples the way you do when the project is complex, multidisciplinary, team and will require a lot of maintenance. Knowing how to make the complex helps make it simple as long as you do not lose sight of the simplified form. A pity that many programmers end up adopting the complex even in cases that do not need all this. So I give reason to the teacher too, I just think he should have explained why, teachers are good for this. It may be that he left to turn around, good teachers also do this.

The answer is in the question. The separation helps each file have its responsibility, so one looks after the page and another takes care of the behavior it will have. This helps to test, service and even trade for another component if it is well done and gives scope for part exchange.

It does not mean that HTML can not have anything PHP, it can be necessary to mount the page, it has to be very simple, but not to manipulate data, to do real processing, to take care of business rules.

Understand the whole to make your own decisions.

    
05.09.2017 / 02:39
1

Complementing the answers, it is worth remembering that HTML files do not support PHP files, put PHP files are server side, and HTML files are Client side. Maybe that's what your teacher wanted to talk about.

Link: DIFFERENCE BETWEEN SERVER-SIDE AND CLIENT-SIDE

One note, PHP files can be fully assembled without having any PHP code inside it, only with pure HTML, what changes, is that at any time, I can put PHP code in it. Ja HTML files, can not contain anything PHP, put will not be run, the .html file does not understand that code in PHP.     
05.09.2017 / 02:48
1

Friend, sooner or later you'll have to integrate both languages unless you do not want a dynamic page! however, it is advisable to reduce as much as possible the "blending" of languages, in fact you will separate the responsibilities! as well ? example: you want to save an information in a database.you will need to make connection with it, obviously! You can just play the php code in the middle of the html code, but this is NOT a good practice! the ideal is to create a new file in the msm folder (ex: banco_banco.php), and there you put all the functions and variables related to the connection, then in html you only do one 'include' banco.php. so it's as if you put the code there, but it will not, so it's easier for you to work on html, and your code gets more organized! understood ?

    
05.09.2017 / 15:41
1

Correct. PHP files, worth noting, require a lot from the server. The ideal is to build your entire project in html, ajax and jquery, making request to the PHP that is located externally, in another folder. That way, the requirement to server will drastically reduce. This is the real reason not to include PHP directly in the HTML page. I hope I have helped.

    
22.09.2017 / 17:27