ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

19 – PHP Require

Require command is used to include a file into your PHP code. You will notice that it’s a bit the same with my previous lesson which is the Include command. But they are not alike, there is a big difference between require and include commands.

 

Require command vs include command

 

An error message will appear when you include a file using the include command and PHP can’t find it.

 

See example Below:

 

Include Code:

<?php
include("noFile.php");
echo "Howdy World!";
?>

 

Display:

Warning: include(noFile.php) [function.include]: failed to open stream: No such file or directory in D:\xampp\htdocs\test\index.php on line 2
Warning: include() [function.include]: Failed opening ‘noFile.php’ for inclusion (include_path=’.;D:\xampp\php\PEAR’) in D:\xampp\htdocs\test\index.php on line 2
Howdy World!

You will notice that the echo statement which is the “Howdy World” is still performed, it is because the Warning doesn’t hinder the PHP script from running. But, if we use the require statement with the same example we would get something like this.

 

See example below:

 

Require Code:

<?php
require("noFile.php");
echo "Howdy World!";
?>

 

Display:

Warning: require(noFile.php) [function.require]: failed to open stream: No such file or directory in D:\xampp\htdocs\test\index.php on line 2
Fatal error: require() [function.require]: Failed opening required ‘noFile.php’ (include_path=’.;D:\xampp\php\PEAR’) in D:\xampp\htdocs\test\index.php on line 2

Notice that the echo statement which is the “Howdy World” was not performed, this is because our script execution didn’t run anymore after a fatal error returned by the require command. So, it’s better to use a require command instead of the include command because your PHP scripts should not be performing if important files are misnamed or lost.

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!