Using Includes with PHP
         by Jubba

Introduction
This tutorial is just a down and dirty explanation on how to use the include() function in an attempt to make your PHP code-writing life a little easier. Basically the include() function includes the file placed inside the parenthesis as the argument inside of your main file. I use this a lot when I am connecting to a database over multiple files. I keep all of my connection variables in my include file and call them throughout my main scripts. Kind of like this:

Include file: include.php
<?
// This is my include file
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$dbName = "MyDataBase";
?>

This is the file that I name include.php Its the one that I will be placing inside of my main scripting files. Then all you need to do to place your information inside of your file is one small line of code:

Main file:
<?
include("include.inc");
// The rest of my code would go here...
// and here...
// and here...
// and here...
// and here...
?>

Now there is another function similar to this, require() the only difference between them is that require() returns a fatal error and stops the script from executing. Include() doesn't do that; the script continues to run even if the file that you are including contains some type of an error. I know that this tutorial didn't cover 100% of the bases. It was just a simple explanation. If you would like more info you can check out PHP.net or feel free to ask on the forums in the Server-side Scripting Forum.

Jubba

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.