How to include files with PHP

by Vivek Gite on September 28, 2006 · 0 comments

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.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 8 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.



Previous post:

Next post: