Difference between require and include in php

Source: Internet
Author: User
Tags php code php tutorial

Include () or require () function. You can insert the content of a file into the file before the server executes the php Tutorial file. Except for the way they handle errors, the two functions are the same in other aspects. The include () function generates a warning (but the script continues to execute), and The require () function generates a fatal error (the execution will be stopped when an error occurs ).

<Html>
<Body>

<? Php include ("header. php");?>

<H1> welcome to my home page

<P> some text </p>

</Body>
</Html>

"Default. php", "about. php", and "contact. php" all reference the "menu. php" file. This is the code in "default. php:

<? Php include ("menu. php");?>

<H1> welcome to my home page

<P> some text </p>

</Body>
</Html>


 

Require () function
The require () function is the same as include (), but the difference is that it handles errors.

The include () function generates a warning (but the script continues to execute), and The require () function generates a fatal error (the execution will be stopped when an error occurs ).

If an error occurs when you reference a file through include (), an error message similar to the following is returned:

Php code:

<Html>
<Body>

<? Php
Include ("wrongfile. php ");
Echo "hello world! ";
?>

</Body>
</Html> error message:
Warning: include (wrongfile. php) [function. include]:
Failed to open stream:
No such file or directory in c: homewebsitetest. php on line 5

Warning: include () [function. include]:
Failed opening 'wrongfile. Php' for future Sion
(Export de_path = '.; c: php5pear ')
In c: homewebsitetest. php on line 5

Hello world!

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.