PHP include and require

Source: Internet
Author: User
Tags php file php code

PHP Include Files

The server side includes
You can insert the contents of the file into the PHP file before the server executes it, with the include () or request () 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 to all of your pages, including. The head needs to be updated, you can only update one of the included files, 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, including the use of features.

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

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

Example 2
Now let's assume that we have a standard menu file that should be used for all pages (including files usually available). "PHP" extension). Look at the "menu.php" file as follows:

<body><a href= "http://www.w3schools.com/default.php" >Home</a> |
<a href= "http://www.w3schools.com/about.php" >about us</a> |
<a href= "http://www.w3schools.com/contact.php" >contact us</a>

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

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

If you look at the source code "default.php" in the browser, it will look like this:

Of course, we also have to do the same thing as "about.php" and "contact.php". By using the Include file,

You only need to update the text in the "menu.php" file if you decide to rename or change the order of the contacts or add other Web pages to the site.

The Require ()

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

If you add files, including () functions, and errors occur, you may get an error message similar to the one below.

PHP Code:

<body>

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

</body>

Error

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 inclusion
(include_path= ' .; C:php5pear ') in c:homewebsitetest.php on line
5
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 requirements () function.

PHP Code:

 

  <body>

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

</body>

  Warning:  require (wrongfile.php) [function.require]: Failed to open stream:no such file or directory into c:homewebsitetest.php on line 5 
  Fatal error: require () [function.require]: Failed opening required ' wrongfile.php ' (include_path= '.;
    C:php5pear ') in c:homewebsitetest.php on line 5 
   
 
           
             

This is the recommended () function rather than including (), because the script should not continue executing if the file is missing or named incorrectly.

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.