The difference between include and require in PHP

Source: Internet
Author: User
Tags php tutorial vars

The main focus is on the red tag statement.

The include (or require) statement takes all the Text/code/markup this exists in the specified file and copies it into the File that uses the include statement.

Including files is very useful if you want to include the same PHP, HTML, or text on multiple pages of a website.

PHP include and Require statements

It is possible to insert the content of a PHP file into another PHP file (before the server executes it), with the INCLU De or require statement.

The Include and require statements are identical, except upon failure:

    • Require would produce a fatal error (E_COMPILE_ERROR) and stop the script
    • Include would only produce a warning (e_warning) and the script would continue

So, if you want the execution-go on and show users the output, even if the include file is missing, use the include STA Tement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the Require statement to include a K EY file to the flow of execution. This would help avoid compromising your application's security and integrity, just in-case one key file is accidentally mis Sing.

Including files saves a lot of work. This means the can create a standard header, footer, or the menu file for all your Web pages. Then if the header needs to is updated, you can only update the header include file.

Syntaxinclude ' filename‘;

Or

Require ' filename‘;
PHP include Examplesexample 1

Assume we have a standard footer file called "footer.php", which is looks like this:

<?php
echo "<p>copyright &copy; 1999-". Date ("Y"). "W3schools.com</p>";
?>

To include the footer file with a page, use the Include statement:

Example<body>

<p>some text.</p>
<p>some more text.</p>
<?php include ' footer.php ';? >

</body>
Run example?
Example 2

Assume we have a standard menu file called "menu.php":

<?php
Echo ' <a href= "/default.asp" >Home</a>-
<a href= "/html/default.asp" >html tutorial</a>-
<a href= "/css/default.asp" >css tutorial</a>-
<a href= "/js/default.asp" >javascript tutorial</a>-
<a href= "Default.asp" >php tutorial</a> ';
?>

All pages of the WEB site should use this menu file. Here's how it can do (we are using a <div> element so, the menu easily can be styled with CSS later):

Example<body>

<div class= "Menu" >
<?php include ' menu.php ';? >
</div>

<p>some text.</p>
<p>some more text.</p>

</body>
Run example?
Example 3

Assume we have a file called "vars.php" with some variables defined:

<?php
$color = ' red ';
$car = ' BMW ';
?>

Then, if we include the "vars.php" file, the variables can is used in the calling file:

Example<body>

<?php include ' vars.php ';
echo "I have a $color $car.";
?>

</body>
Run example?
PHP include vs. require

The Require statement is also used to include a file into the PHP code.

However, there is one big difference between include and require; When a file was included with the include statement and PHP cannot find it, the script would continue to execute:

Example<body>

<?php include ' nofileexists.php ';
echo "I have a $color $car.";
?>

</body>
Run example?

If We do the same example using the require statement, the Echo statement would not be executed because the script Execution dies after the Require statement returned a fatal error:

Example<body>

<?php require ' nofileexists.php ';
echo "I have a $color $car.";
?>

</body>
Run example?
Use require when the file was required by the application.

Use include when the file was not required and application should continue when file was not found.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.