Jump to content

[PHP] Include(_once) vs Require(_once)


Recommended Posts

I'm just trying to figure out what exactly goes on when I put these into my PHP files. At first reaction, they're very similar, both incorporating the target file into the current file.

Just a few questions though:

1) If an include fails, will the script continue? What about for require? (I'm guessing no)

2) Is the code from the included file simply injected into the current file, or is it parsed?

3) Is one preferable to the other?

I've been playing around with some basic PHP, but I seem to be getting stuck on various places where I'm including header files or general helper functions.

Thanks in advance. :)

Link to comment
Share on other sites


1) It will depend on what the to-be-included file contains, e.g. if it contains a vital variable that the script depends on, it will most likely fail to execute. But this depends entirely on the situation.

2) The code you include can be two things: 1 - a text file, that will appear on the place where you place the include code. 2- a php file wich needs to be executed, in opposition to a text file, a php file to-be-included does need to have the <? or <?php starting and ?> ending-tags in the file.

3) I don't really know the difference between the include() and require() functions either, I preffer include(), but it could be that require() has a higher level of dependance or something. Check the php.net documentation for that ;)

Edit:

require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
Edited by Martijn
Link to comment
Share on other sites

for the error:

Warning: include(header1.php) [function.include]: failed to open stream: No such file or directory in E:\Program Files\HTML\Apache2\htdocs\coastal\getstarted.php on line 13

but the script will still continue

Link to comment
Share on other sites

As far as im awaare the difference is so.

the require function will halt if the sepcified file is not found, the include function just keeps parsing the file.

Also I think the include will be pasred every time it is seen where require is only parsed once

e.g.

include("somefile.php");

...

include("somefile.php");

will parse the same file twice, once at each include statement

the require statement

require("somefile.php");

...

require("somefile.php");

will only parse somefile.php once and keep the information in the buffer

(I could be wrong on this but I remeber reading it somewhere)

Link to comment
Share on other sites

And you could kill the include using an if statement and using exit(); if you really wanted to. That is normally how I do it. Since I generally send an email out to me when the include fails for whatever reason. But I want the script to continue (well depending on the error code) without the user know there is a problem (when I can help it) .

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...