Difference Between include and require in php

Source: Internet
Author: User
The difference between include and require in php focuses on the red mark statement. Theinclude (orrequire) statementtakesallthetextcodemarkupthatexistsinthespecifiedfileandcopiesit=thefilethatusesthe difference between include and require in php

Focus on the red mark statement.

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

Including files is very useful when 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 one PHP file into another PHP file (before the server executes it), with the include or require statement.

The include and require statements are identical, snapshot T upon failure:

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

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. this will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

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.

Syntax

Include'Filename';

Or

Require'Filename';


PHP include ExamplesExample 1

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

Echo"

Copyright©1999-". date (" Y ")." W3Schools.com

";
?>

To include the footer file in a page, use the include statement:

Example




Welcome to my home page!

Some text.


Some more text.






Run example?


Example 2

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

Echo 'home-
HTML Tutorial-
CSS Tutorial-
JavaScript Tutorial-
PHP Tutorial ';
?>

All pages in the Web site shocould use this menu file. Here is how it can be done (we are using

Element so that the menu easily can be styled with CSS later ):

Example








Welcome to my home page!

Some text.


Some more text.





Run example?


Example 3

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

$ Color = 'red ';
$ Car = 'BMW ';
?>

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

Example




Welcome to my home page!
Echo "I have a $ color $ car .";
?>



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 is wrongly Ded withIncludeStatement and PHP cannot find it, the script will continue to execute:

Example




Welcome to my home page!
Echo "I have a $ color $ car .";
?>



Run example?

If we do the same example usingRequireStatement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:

Example




Welcome to my home page!
Echo "I have a $ color $ car .";
?>



Run example?


UseRequireWhen the file is required by the application.

UseIncludeWhen the file is not required and application shocould continue when file is not found.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.