what is the difference between include() and require() functions ?
difference between include and require
I keep telling people, but they just don't listen :-)
Manuals are your friend!
-- quote --
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.
-- end quote --
Can somebody give real world example. Sometimes doc is not enough. And this is an example of that. Why do you think that everybody is asking that.
[deleted]
a) because many people refuse to read manuals.
b) A real-world example. Of the difference between include() and require() because the difference is unclear... hmm.. oh well..
first
logon.php:
<?
echo "logon";
?>
logout.php:
<?
echo "logout";
?>
main:
<?
if ($action=="logon")
{
include("logon.php");
}
if ($action=="logout")
{
include("logout.php");
}
?>
second
user_lib.php:
<?
function user_lib_logon()
{
echo "logon";
}
function user_lib_logout()
{
echo "logout";
}
?>
main:
<?
require("user_lib.php");
if ($action=="logon")
{
user_lib_logon();
}
if ($action=="logout")
{
user_lib_logout();
}
in the first, the include() file is only read if action==logon or action==logout
in the second, it's allways read, even if action=="hello".
So if your user_lib is 200k, require() will allways read the 200k of code, even if it is never used, where include() will only read it if and when the include() is executed.
but if you switch include and require this code act the same. You make the diference only because in firt you use firt if, the second you use require fist.
I think that this is very bad example
[deleted]
Ofcourse the code acts the same if you switch require and include.
If you read my post a little better you would have noticed that I mentioned
that in the first example, the included file is ONLY READ IF THE LINE WITH THE INCLUDE() STATEMENT IS EXECUTED.
And in the second example, the required file IS ALLWAYS READ EVEN IF NONE OF THE CODE IS NEVER EVER EXECUTED.
Do you get it now?
Wrong
I use php4
I got a file "new.inc"
<br> Hello
<?
global $counter;
echo ++$counter;
?><br>
another phpfile
<?
$counter=0;
require("new.inc");
if(0) require("new.inc");
if(0) require("new.inc");
?>
I expect new.inc to be called thrice but is called only once
The same if I use include
Read this, it is as clear as water: