0% found this document useful (0 votes)
14 views

PHP Tizag Tutorial-22

The require function is used to include files in PHP code. Unlike include, require will produce a fatal error if the file is not found, stopping script execution. Include only produces a warning and allows the script to continue. The document demonstrates examples comparing include and require, showing that require ensures necessary files are present before execution continues, while include does not halt execution if a file is missing.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

PHP Tizag Tutorial-22

The require function is used to include files in PHP code. Unlike include, require will produce a fatal error if the file is not found, stopping script execution. Include only produces a warning and allows the script to continue. The document demonstrates examples comparing include and require, showing that require ensures necessary files are present before execution continues, while include does not halt execution if a file is missing.

Uploaded by

Anil Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

PHP Require Function

Just like the previous lesson, the require function is used to include a file into your PHP code. However
there is one huge difference between the two functions, though it might not seem that big of a deal.

Require vs Include

When you include a file with the include function and PHP cannot find it you will see an error message like
the following:

PHP Code:
<?php
include("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 Warning: main(): Failed
opening 'noFileExistsHere.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in
/home/websiteName/FolderName/tizagScript.php on line 2

Hello World!

Notice that our echo statement is still executed, this is because a Warning does not prevent our PHP
script from running. On the other hand, if we did the same example but used the require statement we would
get something like the following example.

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 function
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.

You might also like