New Blog Format | Hosted on ServerDisk | Site Map
Search:
NewSourceMedia Popular Tutorials: PHP Javascripts Web Hosting
SoftwareTutorials
CATEGORIES
Scripts (2,2351) Podcast (video & audio)
 




Home / PHP / Productivaty

PHP Include and Require Functions

Published by: Alexander Mclean III
Speed up your developing by using PHP include() and require() functions. Learn how to include multiple files into one PHP file and learn why this is useful in this short lesson.



Introduction:

With PHP, there are two ways to include a file. There is the include() statement and the require() statement. Both constructs are very similar except on how they handle failures. Include() produces a Warning while require() results in a Fatal Error.

I prefer to not to use the require() statement, because if there's an error, the rest of the page will not be displayed. Include() does not behave this way, the script will continue regardless and all of the other code will be executed.

When a file is included, any variables available at that line in the calling file will be available within the called file, from that point forward. For example, you may want to store all of your php functions or database username and passwords into a file called "inc_funct.php" and have all the other pages include it. This will save you the headache of having to retype the code over and over again.


Source PHP Code:

<?
// include a file.
include('inc_info.php');
// display the value of the included file.
echo $password;
?>

 

Source PHP Code of Included File (inc_info.php):

<?
$password = "1234";
?>


Conclusion:

Just be sure to properly type the path to your file. Note, if you're including files that are in higher directories (parent directories), it's best to type out the full path name starting from the home directory (ex: home/user/public/file.php).

    Comments

  1. Alex
    02.03.2004

    I think you would use the include_once() and the require_once() function in the case of preventing the inclusions of a file with functions more then once in the same file.

    For an example, if you included file with a database connection function more then once in the same php page, your database connection will be disrupted by an error. If you use include_once(), the second time you include the file, the second db connect functions will be ignored the error will be prevented.
  2. John Ward
    02.02.2004

    Can you expand on the article and explain Include_once() and Require_once(), and how that differs?

Leave a Comment



Related Articles for PHP Productivaty:
Articale PHP Include and Require Functions
Speed up your developing by using PHP include() and require() functions. Learn how to include multiple files into one PHP file and learn why this is useful in this short lesson.
Articale POST/GET Form Variables
Using PHP array functions to view all of your POST/GET Variables make it easy for retrieving data from online forms. It's also great for debugging your codes.
Articale Array: Lesson Part 1
Learn PHP arrays and how easy it is to store and organize complex data structures.
Articale Installing PHP MySQL Apache on windows 98 in 5 min
This article on "Installing Apache-MySQL-PHP on Windows 98" in just five min is a complete step by step tutorial to install this trio package. The article contains full graphics snapshots to help the new developers to install the trio package without any
Articale List of Top Web Hosting sites with
UNLIMITED DISK SPACE





Hosting - List of top Web Hosting Services with FTP and Unlimited Disk Space