简体   繁体   中英

Can i use PHP file to contain HTML data. Or should i seperated them?

I am doing the group assignment that involves using HTML, CSS, JavaScript and PHP. I am using seperated HTML, then send the form data to a seperated PHP file to handle. But the more I learn about PHP, I feel that I can use pure PHP file to contain HTML data too, more convenient.

For example: I have a html file login.html , it has one <form> element inside the body to login. The form use POST and it leads to processLogin.php

Should i process them separately or to use single processLogin.php that contain a form and contains the php scripts too?

Mixing languages is not a good idea. Because different languages usually correspond to different layers.

PHP deals with business logic. HTML deals with presentation of the business objects. Thus, you have two layers: the one generates the objects; the other one builds a pseudo-XML representation of those objects.

In the same way, JavaScript deals with interaction, and CSS with the presentation of the content. When you want to change interaction, you don't open an.html file, and you surely don't open a.php file: you open a.js or.coffee file.

Note: extracted form other answer. You can read the full answer here .

Now, my opinion. For a small project u can use together, but if want a robust project try use the MVC pattern . The main aim of MVC Architecture is to separate the Business logic & Application data from the USER interface.

it would be better to separate them, write the html in one file and php in another file, so that will be easy if you want to change something in php code or html, the same for css and js it would be better to write everything separately...

Mixing PHP with HTML



Do not echo out HTML!

If you plan on taking the “path to recovery”, then this is the first rule that you should take on board. Echo'ing out HTML is horrible and it prevents non-programmers from being able to work with your design, It also prevents your IDE from being able to highlight HTML elements, attributes, ID names and class names!

Do not mix your logic with your presentation!

There are four terms that you need to be aware of:

  1. Logic : This refers to the business logic of your application. Examples include database connections, functions, SQL queries, redirects, session handling and other pieces of logic that control the flow of your application.
  2. Presentation : This refers to the appearance / client-side of your application. We're talking about HTML, CSS and in some cases, JavaScript.
  3. Presentation Logic : This is the type of logic that determines the appearance of a certain piece of data. For example, if you are outputting a list of registered users, you might want to highlight disabled accounts in red. This type of logic does not control the flow of your application and it plays no part in any of the business logic! It's only purpose is to control the layout and appearance of your application!
  4. Separation of concerns : Simply put, “Separation of concerns” is a design principle that splits an application up into its relevant “categories (concerns).” Example: Code that handles database connections and SQL queries will be kept separate from the part of the application that handles requests and redirects. In this particular case, we'll keep it simple by saying that we're talking about the act of keeping our logic separated from our presentation!

Why should you separate them?

  1. File sizes become smaller.
  2. You don't have to worry too much about designers damaging the internal workings of your application. They can add design features without having to look at the business logic.
  3. It is much cleaner and easier on the eyes.
  4. Bugs are easier to hunt down.
  5. Your business logic will become easier to modify (adding new features, etc).
  6. Re-designs are much less of a hassle.
  7. It is easier to find certain parts of the application if know where everything is.

MVC Compliance

MVC (Model–View–Controller) frameworks are popular because they help to enforce the separations of concerns principle (they also come with a lot of useful libraries and helper classes). Generally speaking, an MVC framework will split your application up into three separate parts:

  • Model: The Model represents the data. Examples: A MySQL table or an XML file.
  • View: The View displays data (presentation). In most cases, view files will consist of HTML and some basic presentation logic.
  • Controller: The Controller passes data to the view. It receives form data from the view and it controls the flow of your application (redirects, etc).

Template Engines

Another alternative is to use a template engine such as Twig, which will allow you to separate your logic from your appearance. In a lot of cases, these templating engines are actually used inside some of the MVC frameworks that were listed above! A cool thing about Twig is that is actually supports view inheritance!

Summary

If you don't have the time to learn about using an MVC framework or a templating engine, then you should at least organize your application into its relevant parts / folders: Examples:

  1. Separate your HTML from your business logic.
  2. Keep your database connection code in one place. Use the required statement to include it in your scripts.
  3. Learn about object-orientated programming.
  4. Try to keep your SQL queries separated from the parts of your application that handle requests, redirects, and form data.
  5. Do NOT echo out HTML with PHP.
  6. Keep your folders organized and be consistent!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM