Hi All,
I have started learning PHP now.
I am unable to find the exact difference between include and require in php?
The require() function is identical to include(), except that it handles errors differently.
The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).
check this :
http://www.tizag.com/phpT/require.php
and also check this:
---------------------------------------------------------------------------------------
PHP provides four functions which enable you to insert code from other files.
* include()
* require()
* include_once()
* require_once()
All four can take a local file or URL as input. None of them can import a remote file.
require() and include() functions are virtually similar in their function except for the way they handle an irretrievable resource. include() and include_once() provide a warning if the resource cannot be retrieved and try to continue execution of the program if possible. require() and require_once functions provide stop processing the page if they cannot retrieve the resource.
Why include_once() and require_once()
The include_once() and require_once() functions are handy in situations where multiple files may reference the same included code. For example:
File A.php includes File B.php and C.php
File B.php includes File C.php
File C.php has been included twice, so the interpreter would print an error. Since a function cannot be redefined once it’s declared, this restriction can help prevent errors.
If both File A.php and File B.php use include_once() or require_once() to import File C.php, no errors would be generated. PHP would understand that you only want one instance of the code in File C and would not try to redeclare the functions.
It is best to use require_once() to include files which contain necessary code and include_once() to include files that contain content which the program can run without e.g. HTML, CSS, etc
hey;
I had been using the include and require if i want to have a continue execution. but i also use the include_once and require_once if I want the execution once only.
hope this help you.
Thanks for your reply.
Another query is that is there any differnce in the opcode generated?
Is it something that the opcode for require is generated when the keyword is encountered and for include is generated when the function related to the file which has been included is called??
Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.
In PHP 3, it is possible to execute a return statement inside a require()ed file, as long as that statement occurs in the global scope of the require()ed file. It may not occur within any block (meaning inside braces ({}). In PHP 4, however, this ability has been discontinued. If you need this functionality, see include().
Difference between "require()" and "include()" in PHP
This should be well known, and people should be aware as to why they are using either or. But, I've noticed lately that a lot of people new to PHP or programming are not aware of the difference. Depending on what you need you, need to decide what the differences are. So...
* require() : If the file does not exist, you will get a fatal error.
* include() : If the file does not exist, you will get a warning and the next line of code will execute.
Unlike include(), require() will always read in the target file, even if the line it's on never executes. If you want to conditionally include a file, use include(). The conditional statement won't affect the require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed.
In PHP 3, it is possible to execute a return statement inside a require()ed file, as long as that statement occurs in the global scope of the require()ed file. It may not occur within any block (meaning inside braces ({}). In PHP 4, however, this ability has been discontinued. If you need this functionality, see include().
This means that if i give
if(5>6)
include "a.php";
else
include "b.php";
only b will be included ..and if i say
if(5>6)
require "a.php";
else
require "b.php";
both a.php and b.php are included?
yep...
see this example...
$value = true;
if($value == true)
{
echo "alright";
}
else
{
require("error.php");
}
// error.php will be included, even when $value is true
hello sorry for my bad post above...its damn wrong...
my code is based on if condition ,not on require function...
so please kindly ignore the above post...please..
It is absolutely ok Shanti.. Thanks a lot your answers.
But Please explain me whether my understanding is correct as mentioned in the post above yours??
Hi All,
I have started learning PHP now.
I am unable to find the exact difference between include and require in php?
include can include more files if file will be same than include return warning error but require can't include same file if u included same name of file then require return fatal error.....
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.