Explanation of the PHP Include file

Source: Internet
Author: User
Tags php tutorial vars
Include in PHP to get the file has important role, so this article will explain in detail its related knowledge.

Server-side include (SSI) is used to create functions, headers, footers, or elements that can be reused on multiple pages.

The include (or require) statement gets all the text/code/tags that exist in the specified file and is copied to the file that uses the include statement.

The include file is useful if you need to refer to the same PHP, HTML, or text on multiple pages of the site.

PHP include and Require statements

With the include or require statement, you can insert the contents of a php file into another PHP file (before the server executes it).

The Include and require statements are the same, in addition to error handling:

Require generates a fatal error (E_COMPILE_ERROR) and stops the script

Include only generates a warning (e_warning), and the script continues

Therefore, if you want to continue execution and output the results to the user, use include if the included file is missing. Otherwise, in framework, CMS, or complex PHP application programming, always use require to reference critical files to the execution stream. This helps improve the security and integrity of your application in the event that a critical file is accidentally lost.

The inclusion of files saves a lot of work. This means that you can create a standard header, footer, or menu file for all pages. Then, when the page header needs to be updated, you only need to update the header include file.

Grammar

Include ' filename ';

Or

Require ' filename ';

PHP include instance

Let's say we have a standard footer file called "footer.php", just like this:

<?php
echo "<p>copyright 2006-". Date ("Y"). "W3school.com.cn</p>";
?>

To refer to this footer file in one page, use the Include statement:


Let's say we have a standard menu file named "menu.php":

<?phpecho ' <a href= "/index.asp" > Home </a>-<a href= "/html/index.asp" >html tutorials </a>-<a href= /css/index.asp >css Tutorials </a>-<a href=/js/index.asp >javascript Tutorials </a>-<a href= "/php/ Index.asp ">php tutorial </a>";? >

This menu file is used for all pages in the site. The specific approach is (we use a <div> element so that we can easily set the style through CSS in the future):


Suppose we have a file named "vars.php" that defines some variables:

<?php$color= ' silver '; $car = ' Mercedes-Benz Sedan ';? >

Then, if we refer to this "vars.php" file, we can use these variables in the calling file:


PHP include vs. require

The Require statement is also used to refer to a file in PHP code.

However, there is a huge difference between include and require: If a file is referenced with an include statement and PHP cannot find it, the script continues:


If we use the Require statement to complete the same case, the Echo statement will not continue because the script terminates execution after the require statement returns a critical error:


This article explains in detail the use of include and the difference between the require, more learning materials to pay attention to the PHP Chinese network can be viewed.

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.