include() and require() functions

Muhammad Haris (PHP / Moderate)

These are few common functions which are used in most of the scripts to manage the scripts easily. Please take a look carefully over the most common function which is :-

include() & require() functions :

Basically include() and require() are going to do the same thing. They both are going to read any file as php and (x)html, even if it's a .inc or .txt file and parse it.

The major difference between both of them are that include() produce warning errors whereas require() produce fatal error. You should use require() if you want to halt the full script if the file which is required() is not found.

Here is an example of require() function. First I will create a mysql connection string file and use the require() function to include it in my normal pages.

<?php

// database.php

$db = mysql_connect("localhost", "username", "password"); mysql_select_db("databasename", $db);

?>


Here is the the normal php page where I will use the require() function to include database.php

<?php

// normal.php

require "database.php";

echo "This is a test require() function php file";

?>


The above code will produce no error because the file database.php is not missing else it would end the script and produce the fatal error whereas in the above example of if you use include() function instead of require() function it would continue to execute the file and produce some warning error even if the file database.php is not included properly.


© Copyright 2006 - All Rights Reserved - SiteGuts is a part of the Enthonia Content Network