PHP in the same file as the form or separately? speed

I just started learning PHP and just did with $_POST / $_GET . Now I want to know what pro and con are for PHP to process data from a form inside the same file or send data to another file ( action="anotherfile" )?

Logically, I will think that sending it to another file will increase processing time, but is this true? When I have a PHP script inside the same file, the page does not seem to reload when I click the submit button (but the contents change). Or that? If this happens, wouldn't it be the only difference that I would need to enter a script for the menu (let's say you have the same menu on all pages) in both files? What will lead to more coding / less space?

+5
source share
4 answers

What is pro and con for PHP to process data from a form inside the same file or send data to another file (action = "anotherfile")?

You combine files and URLs.

By dividing the logic between different files (and then include d, where necessary), you separate problems and simplify code management.

Due to the fact that one URL is responsible for displaying the form and processing the form data, you do not find yourself in an uncomfortable situation when the result of processing the form data requires that you re-display the form with error messages in it. If you used two different URLs, you will need to either display the form on the processing URL (so you have two different URLs that display the form), or redirect the HTTP back to the original URL, then while transmitting error details.

Logically, I will think that sending it to another file will increase processing time, but is this true?

No. It doesnโ€™t matter what time scales it handles.

When I have a PHP script inside the same file, the page does not seem to reload when I click the submit button (but the contents change).

It reboots.

If so, wouldn't it be the only difference that I would need to enter a script for the menu (let's say you have the same menu on all pages) in both files?

Why include .

+11
source

In any language, we always try to write clean code. That is why we are following MVC.

Logically, I will think that sending it to another file will increase processing time, but is this true? I think not.

Because when we send data to another page, and on another page at the top, we echo the data and exit. You will see that it does not take much time. it will take time when we redirect / load some html page after that.

It doesnโ€™t matter where we send the data (on the same page or on another page). matter is what is loaded after that.

+2
source

There is no difference in speed.

You send the content of your form to the standard submit, this data will be sent to the server and the response will be uploaded (after processing).

The only difference is the organization of your code. The logic that draws the page of the page (menu or other fixed parts) must be stored in some file that you can include separately or call a function.

It is also true that when you publish your data for some reason, register a user, for example. It is a good estimate that a php file that handles user registration will do this and display messages, not other functions. If your file has some logical switches that display either an empty form or a registration message based on the presence of messages or variables, you will notice that when scaling more complex tasks this will complicate the situation and complicate the work of the code.

+2
source

I will try so that I understand your question by reformulating it.

If you have a form ( /form.php ), and the "action" of this submit button will lead you to a separate php page ( /form_action.php ), then there is no difference in speed. Each HTTP request ( form.php and form_action.php ) is independent - "form_action.php" remembers nothing about "form.php" if you do not pass this information through (as parameters). This is what people mean when they say that HTTP is stateless . It is worth learning about how HTTP generally works along with the details of PHP.

If you have a PHP script that, in turn, includes other PHP scripts, the tiny performance impact is too small to measure anyway anyway I have ever come across.

However, using include allows you to share markup (HTML) with logic (PHP). It is really good if you do something other than bother. This allows you to reuse functionality, it makes it much easier to change and maintain your code over time, and it helps you think through what you are trying to achieve.

There are many different ways people decide "how to keep my code clean"; current orthodoxy "Model-View-Controller" (as @monty says). There are also PHP frameworks that make this a little easier to implement - as soon as you learn the basics of the language, you can look at Zend or TinyMVC (there are several others, each of which has its advantages and disadvantages).

+1
source

Source: https://habr.com/ru/post/1240337/


All Articles