Zxian Posted June 22, 2006 Share Posted June 22, 2006 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 More sharing options...
Martijn Posted June 22, 2006 Share Posted June 22, 2006 (edited) 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 June 22, 2006 by Martijn Link to comment Share on other sites More sharing options...
Zxian Posted June 22, 2006 Author Share Posted June 22, 2006 Thanks for the answers Martijn. That's pretty much what I had been thinking already - but I wasn't quite sure. Link to comment Share on other sites More sharing options...
ripken204 Posted June 22, 2006 Share Posted June 22, 2006 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 13but the script will still continue Link to comment Share on other sites More sharing options...
phkninja Posted June 23, 2006 Share Posted June 23, 2006 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 oncee.g.include("somefile.php");...include("somefile.php");will parse the same file twice, once at each include statementthe require statementrequire("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 More sharing options...
war59312 Posted June 23, 2006 Share Posted June 23, 2006 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now