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

Asked

Viewed 880 times

4

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

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

  • 5

    Not quite so, maybe the best was even you ask for it, or else post as you made the code ... even so can be answer based on own opinion, at the end of the accounts there is no way to get away from it ... the framework tries to be to the maximum, as a form of organization ... but, says there what you showed to the friend.

  • 2

    You should separate because it is easier to maintain code with each type of code in "your square" or in its due file. In the example you saw in the lab, maybe it didn’t make much sense to separate, because there was little code, but when you go to a corporate environment, there will be a lot more code and this separation is necessary. But I’ve never seen this separation be 100%, I use it myself foreach in HTML file, but I always try to use in less quantity.

  • 3

    Just for the fact that you question this, it’s already started right. Congratulations.

  • 1

    My teacher once said to me: "Whoever asks passes for layman once, whoever does not ask will always be layman!"

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

Show 1 more comment

4 answers

4

I am of yours, in simple things do it as simply as possible.

Of course, if you are learning the ideal is to learn to do even in simple examples of the way you do when the project is complex, multidisciplinary, in a team and will require a lot of maintenance. Knowing how to make the complex helps to make the 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 the teacher reason too, I just think he should have explained why, teachers are for that. It may be that he left it for you to turn around, good teachers also do it, but it doesn’t leave you lost all the time.

The answer is in the question. The separation helps each file have its own responsibility, so one takes care of the look of the page and the other takes care of the behavior it will have. This helps to test, maintain and even swap for another component if it is well done and give room for the exchange of parts.

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

Understand the whole to make your own decisions.

0

Complementing the responses, 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 remark, PHP files can be fully assembled without 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 run, the file . html does not understand that code in PHP.

  • I think there is a certain conflict in its definition. It is necessary to separate "PHP files" and "PHP code". PHP file can be in pure HTML. The extension ". php" only indicates that the file can contain PHP codes.

  • Thanks Dvdsamm! I will edit!

0

Correct. PHP files, worth mentioning, requires a lot of server. The ideal is to build your entire project in html, ajax and jquery, requesting PHP that is located externally, in another folder. This way, the server requirement will drastically reduce. This is the real reason not to include PHP directly in the HTML page. I hope I’ve helped.

0

Friend, sooner or later you will have to integrate the two languages, unless you do not want a dynamic page! however, it is advisable to reduce as much as possible the "mixing" of the languages, in fact you will separate the responsibilities ! how so ? example: You want to save information in a database.You will need to connect to it, obviously! vc can simply play the php code in the middle of the html code, but this is NOT a good practice! the ideal is you create a new file in msm folder (ex: conexao_banco.php), and there in Voce puts all functions and variables related to the connection, ai in html vc makes only a 'include "conexao_banco.php" ' . so it’s like Voce has put the code there,but you won’t see ,so it’s easier to work on html,and your code is more organized! got it ?

Browser other questions tagged

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