Click to See Complete Forum and Search --> : require() vs include() in PHP
Strike
06-07-2001, 12:10 PM
I know there are differences between these two, but I'm not sure I understand them. I know that require() will always preprocess the file and include it (like #include in C/C++), even if it's within a code construct that doesn't get executed, and that this is not the case for include(). And I'm pretty sure there is a difference with respect to include paths, but what are they? Let's take an example. Say I have a file that needs some constants from "constants.inc", and some functions from "functions.inc". Let's assume the directory structure is as follows:
/phpstuff/mydir/test.php
/phpstuff/functions.inc
/phpstuff/constants.inc
So, in test.php, at the top I put:
<?php
require("../functions.inc");
require("../constants.inc");
?>
And at the top of functions.inc I have:
<?php
require("constants.inc");
?>
Well, that doesn't work for me.
I get (well, altered to fit this example):
"Warning: Failed opening 'constants.inc' for inclusion (include_path='.:/usr/lib/php4') in /phpstuff/functions.inc on line 2
Where line 2 is the require("constants.inc"); line shown above.
How do I fix this?
[ 07 June 2001: Message edited by: Strike ]
DaMasta
06-07-2001, 12:19 PM
I was having this problem. It was because it was not in the right directory. ie: I had an index.php which would call a pages/registration.php. Registration.php would call another php file. I was putting pages/anotherphp.php where it should have just been anotherphp.php. I guess what I'm trying to say is that from where the file gets called, make the path relative to it's directory. I hope this helps. You may have already checked this out though.
Strike
06-07-2001, 12:31 PM
Well, the problem is, "constants.inc" is being called by a file that will be called by several different files. It will always want to be included with "functions.inc", but "functions.inc" won't necessarily always be called by a file in "mydir".
Basically what I'm asking for is a way to make include path relative to the file that the require() or include() call is being made from, like it is with the C preprocessor.
Sweede
06-07-2001, 06:14 PM
use full paths, there is no difference between include() and require()
http://www.php.net/manual/en/function.require.php
few things,
its best to make a single file, lets say 'prepend.php' that contains like so..
<?php
$doc_root = '/home/joe/htdocs/include';
include($doc_root."/config.php");
include($doc_root."/constants.php");
include($doc_root."/functions.php");
?>
then in every file OUTSIDE the include directory, use
include("prepend.php");
DONT include other files in the files in the include/ directory, this will confuse things and causs bugs (trust me, it does. ever use php-nuke?).
Strike
06-07-2001, 11:18 PM
Well, that sucks. And there IS a difference between the two (why do you think they have both), but I guess that neither one will do what I want it to.
Sweede
06-08-2001, 03:56 AM
the diff between include and require ??
require includes the contents un-conditionally, meaning whether or not an if statement was true, the file would still be read.
a good example..
<?php //file.php
$foo = 'hello';
?>
------------------------------------
<?php
if ($var == true) {
reqiure(file.php);
} else {
echo $foo;
}
?>
would echo hello if $var is false.
<?php
if ($var == true) {
include(file.php);
} else {
echo $foo;
}
?>
if $var was true, nothing would be echo'd, but file.php would be included..
if that makes sense.
Salmon
06-08-2001, 09:10 AM
HAL,
One thing you could do would be just to alter the include path. Then you could dispense with using the path to the file altogether and just use 'require('constants.inc');' regardless of the location of the file that needs the include. (Hopefully that made a small amount of sense.)
To alter the include path, you can either make changes in your php.ini (which is probably not the best choice) or you can set it manually in your script:
$origIncPath = ini_get('include_path');
ini_set('include_path', "/new/include/path:$origIncPath");
Then you don't have to worry about the proper correct path to the files you want to include.
Hope that helps.
[ 08 June 2001: Message edited by: Salmon ]
Salmon
06-08-2001, 09:31 AM
Originally posted by Sweede:
<STRONG>the diff between include and require ?? . . . </STRONG>
Sweede, that's not entirely accurate.
To quote from the PHP manual:
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.
Just because the file is read doesn't mean that the code within it will be executed.
[ 08 June 2001: Message edited by: Salmon ]