0% found this document useful (0 votes)
68 views3 pages

PHP Include - Require

The document discusses PHP functions for including files. The require() function will include a file and cause a fatal error if it fails, stopping execution. The include() function will include a file and issue a warning if it fails, allowing execution to continue. Both include_once() and require_once() will include a file only once to prevent multiple inclusions, with require_once() causing a fatal error and include_once() issuing a warning if the file is not found. Including files allows code and content to be reused across files.

Uploaded by

gurupatel279
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)
68 views3 pages

PHP Include - Require

The document discusses PHP functions for including files. The require() function will include a file and cause a fatal error if it fails, stopping execution. The include() function will include a file and issue a warning if it fails, allowing execution to continue. Both include_once() and require_once() will include a file only once to prevent multiple inclusions, with require_once() causing a fatal error and include_once() issuing a warning if the file is not found. Including files allows code and content to be reused across files.

Uploaded by

gurupatel279
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/ 3

PHP include and require Statements

PHP require() Function:


 The require() function in PHP is basically used to include the
contents/code/data of one PHP file to another PHP file.
 During this process if there are any kind of error occurs then require()
function will pop up a warning along with a fatal error and it will
immediately stop the execution of the script.

PHP include() Function:


 The include() function in PHP is basically used to include the
contents/code/data of one PHP file to another PHP file.
 During this process if there are any kind of errors then this include()
function will pop up a warning and it will not stop the execution of the
script rather the script will continue its process.

Syntax:
include 'filename';
or
require 'filename';

Note: Including files saves a lot of work. This means that you can create a
standard header, footer, or menu file for all your web pages. Then, when the
header needs to be updated, you can only update the header include file.

Example:

footer.php:

<?php

echo "<p>Copyright &copy; 2021-" . date("Y") . " wt subject</p>";

?>

Welcome.php

<html>

<body>
<h1>Welcome to my home page!</h1>

<p>Some text.</p>

<p>Some more text.</p>

<?php include 'footer.php';?>

</body>

</html>

PHP include_once() Function:


 The include_once() function can be used to include a PHP file in another
one, when you may need to include the called file only one time.
 If it is found that the file has already been included, calling script is going
to ignore further inclusions.

Example: index.php

<?php
include_once('header.php');
include_once('heade.php');
?>

The above file header.php is included twice with include_once() function in


the following file index.php. But from the output, you will get that the
second instance of inclusion is ignored since include_once() function
ignores all the similar inclusions after the first one.

PHP require_once() Function:


 The require_once() function can be used to include a PHP file in another
one, when you may need to include the called file only one time.
 If it is found that the file has already been included, calling script is going
to ignore further inclusions.
 If file does not exist then it stops execution and causing a fatal error.
include_once() vs require_once():

Both functions work as same and produce same output but if any error arises
then differences come.
Example:
If we don’t have a file named header.php, then in the case of the
include_once(), the output will be shown with warnings about missing file, but
at least the output will be shown from the index.php file.
In the case of the require_once(), if header.PHP file is missing, then a fatal
error will arise and no output is shown and the execution halts.

You might also like