Monday 22 April 2013

PHP require


PHP require

Just like the previous lesson, the require command is used to include a file into your PHP code.

However there is one huge difference between the two commands, though it might not seem that big of a deal.

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error.

In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
require will produce a fatal error (E_COMPILE_ERROR) and stop the script.


Syntax:

require 'filename';

PHP Code:

<?php
require("noFileExistsHere.php");
echo "Hello World!";
?>

Display:

Warning: main(noFileExistsHere.php): failed to open stream: No such file or directory in /home/websiteName/FolderName/tizagScript.php on line 2

Fatal error: main(): Failed opening required 'noFileExistsHere.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/websiteName/FolderName/tizagScript.php on line 2

The echo statement was not executed because our script execution died after the require command returned a fatal error!
We recommend that you use require instead of include because your scripts should not be executing if necessary files are missing or misnamed.


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More