How to include files with PHP
Q. How do you include files with PHP?
A. PHP has a special function called include().The include() statement includes and evaluates the specified file.
Foe example if you have a file called lib.php:
<?php
function test(){
echo "test() called";
}
?>
Now you can include lib.php and call test() from demo.php:
<?php
include("lib.php");
test();
?>
Files for including are first looked in include_path relative to the current working directory and then in include_path relative to the directory of current script. E.g. if your include_path is ., current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in include_path relative to the current working directory.
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
Read php include help page for more information.
E-mail this to a friend
Printable version
Related Other Helpful FAQs:
- Why my Apache Server Side Include (SSI) is not working?
- Howto: PHP setup include_path
- Apache Web Server Cache Frequently Used Files To Improve Performance
- How do I find the largest top 10 files and directories on a Linux / UNIX / BSD filesystem?
- x86_64 Linux Error: gnu/stub-32.h missing error and solution
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!



Recent Comments
Yesterday ~ 3 Comments
09/06/2008 11:03 pm (2 days ago) ~ 12 Comments
09/06/2008 02:10 pm (2 days ago) ~ 7 Comments
09/06/2008 06:51 am (2 days ago) ~ 2 Comments
09/06/2008 01:28 am (2 days ago) ~ 3 Comments