I am new to php . I want to know what are the differences between require and include, include_once and require_once?
The first difference between require
and include
is that require
will halt the execution at the line of the error. include
does not and will continue to execute regardless of errors. So basically, say we use a require
and there was a typo in the require
. require
will halt the execution and whichever lines that require
is on, you won’t see the rest of the codes after it. You will also get an error saying there is no such file or directory. However, with include
, say you have the typo. include
will continue to execute the rest of the code. Here is a small example.
Pretend this is a typo for our included file. We’re trying to include next.php
<!DOCTYPE html>
<head>
<title>Next</title>
</head>
<body>
<h1>Our next file</h1>
<?php include('nest.php'); ?><!-- Remeber, we are trying to include next, not nest -->
<!-- Include will execute the lines below regardless if the file has a typo or not -->
<p>I really like <?php print($var); ?></p>
<p>I guess I don't actually like <?php print($var); ?></p>
</body>
</html>
Now. This can be a problem because we are referencing the variable $var
which it was supposed to be defined in the next.php
file. Using include
will do this and once PHP
executes the file, you will get an undefined index
error because you are trying to reference a variable that does not exist even though you know for sure that the variable does exist within the next.php
file.
Here’s require
.
<!DOCTYPE html>
<head>
<title>Next</title>
</head>
<body>
<h1>Our next file</h1>
<?php require('nest.php'); ?><!-- Again, remember that we are calling for next, not nest. In fact, you won't even see this message -->
<!-- The comment above, this comment, and the rest of the below lines will not be outputted to the screen. -->
<p>I like <?php print($var); ?></p>
<p>I guess I actually don't like <?php print($var); ?></p>
</body>
</html>
So require
will not execute any lines following the error. This is a good thing because using the error we know from the include
part, that pretty much, we won’t see this undefined index
error because the execution halts where the typos begin. Once we fix the typos, the included file will be include, thus allowing us to properly reference the $var
variable and thus allowing us to go on our marry way.
Granted, both functions includes a file. But include
has been abussed in PHP
simply because it’s the word “include”. And people would rather remember “I need to include this file, so I use the word include because it would be the logical thing to do.” But it’s actually not.
Now, the difference between slapping a _once
after the include
and require
function isn’t really that different. It still includes the files like both functions do. The only difference is that it will include the file once if it’s being written more than once.
<?php include_once('next.php'); ?>
<?php include_once('next.php'); ?>
So the above lines will only use one of the include lines. I don’t recall which one it uses. But it will not include the file if it is already called once. Same deal goes for require_once
.
There is a time difference in includes and requires.
Welcome to the world of coding Bradley!
To help you along on your journey, allow me to introduce to you the Php Manual where you can gain a lot of useful information on php, require and include.
As you will see from the manual about require, it answers your question exactly.
require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
As well as require_once and include_once.
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.
Here is some good information that will help you.
This link should also be useful as well.
You can do away nearly completely with using those methods directly by using a dependency manager with autoloading like composer in php. In all modern programming methodologies dependency management with naming conventions like PSR-* in php with autoloading are preferred over direct usage of require and include to load files and classes. This might be to advance for your current level of understanding but it is something to keep in the back of your mind. Anyone using those methods (require and include) extensively is writing outdated, legacy code. In the current climate where modern JavaScript has eaten away at the usage of php it is important to be using the most up to date methodologies in the language for new projects.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.