PHP Include and require tutorials

Source: Internet
Author: User
Tags php code require

Server-side include (small industry) used to create features, headers, footers, or content that will be reused for multiple pages.


Server-side Include
You can insert the contents of the file into the PHP file before the server executes it, with the include () or require () function. These two functions are the same in various ways, but how they handle errors. The Include () function generates a warning (but the script will continue to execute), and the () function needs to generate a fatal error (and the script will stop the error after execution).

These two functions are used to create features, headers, footers, or content that can be reused for multiple pages.

This can save the development of a considerable amount of time. This means that you can create a standard title or menu file that you want all of your pages, include. The head needs to be updated, you can only update an include file, or when you add a new page to your site, you may change the menu file (instead of updating all linked pages).


In the Include () function
All the text in the Include () function specifies the file and copies it to the file, include the use function.

Example 1
Suppose you have a standard header file called "header.php". Include header files on a page, using the Include () function, like this:

<?php include ("header.php");?>
<p>some text</p>
</body>

These three files, "default.php", "about.php" and "contact.php" should all include "menu.php" files. The following is the code "default.php":

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

Output.

Now let's look at the Require function www.111cn.net/

The Require () function is the same, include (), just a different processing error.

The Include () function generates a warning (but the script will continue to execute), and the () function needs to generate a fatal error (and the script will stop the error after execution).

If you add a file, include () function, and an error occurs, you may get an error message similar to the one below.

<?php
Include ("wrongfile.php");
echo "Hello world!";
?>

Warning: include (wrongfile.php) [function.include]:
failed to open stream:
No such file or directory in C:homewebsite	est.php on line 5
Warning: include () [function.include]:
Failed opening ' wrongfile.php ' for inclusion
(include_path= ' .; C:php5pear ') in
c:homewebsite	est.php on line 5
Www.111cn.net/phper/php.html
Hello world!
 
    
    

Please note that the echo of the statement is still executed! This is because the alert does not stop executing the script.

Now let's run the same example with the Require () function.

PHP Code:

<?php require ("wrongfile.php"), echo "Hello world!"; "

 

Hint:

p> ning: require (wrongfile.php) [function.require]: failed to open stream:no such file or directory in C: Homewebsite est.php on line 5

  Fatal Error:  require () [function.require]: Failed opening Requir Ed ' wrongfile.php ' (include_path= '.; C:php5pear ') in C:homewebsite est.php on line 5 
 reprint: www.111cn.net/phper/php.html 

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.