Forum Moderators: coopster
My question relates to including files and where it's safe and not safe to assume you have access to the functions and/or variables in those files.
Say I have a page ...
Here's the question: if I want the content of header.php to be available on all the generated pages that come through form_process.php (first the generated preview form, then the generated 'upload successful' message), do I also need to include header.php in form_process.php? Or is the include in the original index.php file enough? Obviously, I can figure this out myself by simply not including it and seeing what happens (my plan once I get back to my dev computer).
I guess my question is actually more general than specific... what are the limits of included files and their contents? At what point to do you lose access to them? Is an included file available only in the file it's included in? Is it available in other files included in that file (so if a.php includes b.php and c.php, are the contents of b.php available to c.php)? Is it available in included included included files (if d.php includes e.php, which includes f.php, can functions in f.php access data in d.php)?
For some reason, I just can't get my head around the logic here. Any basic run down of the limits of includes would be really helpful and much appreciated. I'm starting to feel like I've gone through the looking glass here...
Thanks,
cEM
you could test this easily at home by making several small files with variables in it and including them one after another
e.g.
a.php is
<?php
$var1 = car;
?>
b.php is
<?php
$var1 = boat;
?>
and including both of these in c.php
<?php
include(a.php);
include(b.php);
echo $var1;
?>
etc, etc...
try to imagine all the code in the included file replacing your include function() - they you'll realise that as long as you are on the same original page, all the data from the includes is available. clear as mud? :-)
good luck
Any code brought in using require() is actually added to the program. So anything you require() in index.php becomes part of index.php for all intents and purposes. It would then be available to any other code in the program including code brought in by other require() statements.
require() is generally always better for things like headers, function files and config files. include() is useful if you want to load an external file conditionally or treat an external file like a function that can return a value.
There were a few instances where I had to leave an include in what seemed like and odd place (like in the midst of the function that generates the 'successful post' message, where without an explicit include() the header remained missing. In this case, I left it as an include(), rather than require().
Do you think it matters in that context? (The content of the included file in this case is just a variable holding the html markup for the header.)
require() would include the code even if the function that contains it was never called, whereas include() would include the code only if the function was called.
Nothing wrong with leaving it as include if it works for you though.
requireinstead of
include- but I'll jump in here by noting how the PHP manual describes the difference between
includeand
require:
require() and include() are identical in every way except how they handle failure.- [php.net...]
Using
requirewill actually halt execution if a parse error is encountered in the file required, if the file can't be found, or other biggie errors are encountered in that file. This is good if you're running a sensitive script that absolutely must have a certain functionality for safe execution. I'll typically include all files while programming, to make debugging easier, and change crucial ones to
requirewhen I'm done writing and debugging.