Php the difference between include and require. Which is more correct to use - require or include? Get Blog Updates !!! Subscribe
Often you have to store all sorts of everywhere-used data / functions in separate files, and then include using include [_once] / require [_once]. But these files are usually not parsed by the server, that is, they can be viewed through a browser, which is what we want to avoid. It is not very correct to give such files the PHP extension, since they can be called through the browser, and, although we will not see the content, we will most likely start to get out some errors, since the code inside the files is usually designed to be executed in a certain environment (the presence of a connection to the database reading files / certain values \u200b\u200bof constants / variables). There are 2 outputs that are essentially similar:
- put all INC files outside of DOCUMENT_ROOT Apache
- write (.htaccess) to deny access to all files with certain extensions
Example
# prohibit downloading files with the specified extensionsrequire
Loads and inserts the code into a PHP script. If such a file, the code is not available, the further work of the entire PHP script stops. If the same instruction is called in the same script, then there will be either an error or ignoring the file loading - that is, a void will be stupidly returned.
PHP functions and developed libraries.
Call options:
require "file"; // I like this better, you can see it more clearly require ("file");include
Loads and inserts the code into a PHP script. If such a file, the code is not available, then the main PHP script proceeds to the execution of the code after the include statement. If the file has already been downloaded earlier, it will be downloaded again. You cannot use to load the same modules with functions, there will be an error.
Used primarily for loading HTML inserts into pages.
Call options:
include "file"; // I like this better, you can see it more clearly include ("file");There are also more functions require_once and include_once, the once prefix means loading data into a PHP script only once. If such a function is encountered twice in one script, then the code will be executed only at the first stage, all the others will be ignored, thereby increasing the speed of script execution.
What to use for in PHP files of Include or Require statement?
It is very convenient to split one large program or web page into several files. And it happens that you just need to connect your file to the script of another developer.
I will tell you one incident from my life. When I started learning HTML, I created my first website. At that time, I did not yet know that there was a programming language PHP. The site consisted of 100 pages. When I added an article, I had to specify the name and path of the new page on each page separately in the menu. It was horrible, because, as you know, this lesson took a lot of time until I learned about the Include statement with the ability to include third-party files to my page. As soon as I made a change in one third-party file, for example, in the menu, the replacement took place on all 100 pages. Convenient, fast and fashionable.
Let's try to connect a third-party file to our web page using a simple example.
Create a file named "index.php" with this code:
You can like this:
Now create a file "cop.php" with this code:
Result:
So what have we done?
In the file "index.php" we have connected to the third-party file "cop.php".
If you change the text in the "cop.php" file, then in the "index.php" file you will see the replacement.
Include statement template:
Require statement template:
Another example for pinning the topic.
We have a web page like this:
All rights reserved © 2012-2014..ru
Let's now split this web page into two blocks using the Include or Require statement, as in the diagram:
It will look like this:
Save it under the names "index.php", "avtor.php" and "kontakty.php"
That is, there will be three pages “index.php”, “avtor.php” and “kontakty.php”.
Now create a "menu.php" file for the menu:
Home Author Contacts
All rights reserved © 2012-2014..ru
Result:
If you change something in the "cop.php" or "menu.php" file, then the replacement will automatically occur in the three files "index.php", "avtor.php" and "kontakty.php". Try it yourself.
That's all! Subscribe to the blog update so as not to miss new ones pHP lessons... Good luck!