PDA

View Full Version : What is the difference between include and require?


noRush
Wed 26th Nov '03, 4:39am
What is the difference? When should I use include, require or require_once? Thank you!

merk
Wed 26th Nov '03, 5:04am
include will include the file, and should it fail, keep running the script.

require will include the file, and should it fail, the script will stop.

include_once and require_once are even more handy if you write scripts that run and include eachother, because including() a file more than once will generate errors, but include_once() will only inlcude the file once, regardless of how many times it is called.

noRush
Thu 27th Nov '03, 9:42pm
How does it know if it is the same file? By the file name or what is actually in the file?

Chroder
Thu 27th Nov '03, 10:04pm
Also, require will include the file regardless of control structures etc.


$value = true;

if($value == true)
{
echo "alright";
}

else
{
require("error.php");
}

// error.php will be included, even when $value is true

merk
Thu 27th Nov '03, 10:53pm
How does it know if it is the same file? By the file name or what is actually in the file?
PHP will keep an internal list of files included. Im not sure exactly how it does it, you could check out the PHP source for the function if you need to :)

There might be more explination in the PHP manual.

Faruk
Fri 28th Nov '03, 5:56am
I'm not 100% sure of this at all, but I think PHP just looks at the filename you specify, verifies its location and date, requires (or includes) it, and when a call to the same file is made (it knows which files with what datestamps are included) it checks to see if the date has changed, and if so require it again and if not, doesn't do anything..

That would at least be the logical (and very simple) approach....

i am coo man
Fri 28th Nov '03, 9:14am
Also, require will include the file regardless of control structures etc.


$value = true;

if($value == true)
{
echo "alright";
}

else
{
require("error.php");
}

// error.php will be included, even when $value is true
Not true, require works the same as include and is executed like any other code

merk
Fri 28th Nov '03, 9:46am
I'm not 100% sure of this at all, but I think PHP just looks at the filename you specify, verifies its location and date, requires (or includes) it, and when a call to the same file is made (it knows which files with what datestamps are included) it checks to see if the date has changed, and if so require it again and if not, doesn't do anything..

That would at least be the logical (and very simple) approach....
From memory (i read this in the manual a while ago), php only stores a list of files, it doesnt use dates

Scott MacVicar
Fri 28th Nov '03, 12:58pm
it gets the full path
/path/to/filename.php

creates a hash of the path.

When you do an include_once / require_once it checks the hash against its internal hash table and either ignores or includes again.

Chroder
Fri 28th Nov '03, 4:08pm
Not true, require works the same as include and is executed like any other code
Ah yeah, my bad. Prior to v.4.0.2 ;)

noRush
Fri 28th Nov '03, 11:51pm
So for v.4.0.2 or higher, it only includes when it need to, but any version lower than v.4.0.2, it always includes it no matter what?
Thanks for the support!:)