PHP advanced tutorial (2): PHP Reference File

Source: Internet
Author: User
PHP advanced tutorial (2): PHP Reference File Server Reference (SSI) is used to create functions, headers, footers, or elements that can be reused on multiple pages.

Server Side references)

Using the include () or require () function, you can insert the content of a file into the file before the server executes the PHP 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 ).

These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.

This saves developers a lot of time. This means that you can create a standard header or menu file for reference on all web pages. When the header needs to be updated, you can update only one contained file, or when you add a new page to the website, you only need to modify the menu File (instead of updating links on all webpages ).

Include () functions

The include () function obtains all the text in the specified file and copies the text to the file using the include function.

Example 1

You have a standard header file named "header. php ". To reference this header file on the page, use the include () function, as shown in the following code:
 Welcome to my home page

Some text

Example 2

Now, suppose we have a standard menu file used on all pages. See the following "menu. php ":Http://www.phphtm.com> Home | http://www.phphtm.com> About Us | http://www.phphtm.com> Contact Us three files, "default. php "," about. php "and" contact. php "references" menu. php files. This is the code in "default. php:
 Welcome to my home page

Some text

If you view the source code of "default. php" in your browser, it should be similar to this:Home | About Us | Contact UsWelcome to my home page

Some text

At the same time, of course, we will also use the same method to process "about. php" and "contact. php ". By using a reference file, you can simply update the text in the "menu. php" file when you need to rename the link, change the link sequence, or add another webpage to the site. The require () function is the same as the include () function. 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:

 

Error message:

Warning: include (wrongFile. php) [function. include]: failed to open stream: No such file or directory in C: \ home \ website \ test. php on line 5 Warning: include () [function. include]: Failed opening 'wrongfile. php 'for declaration (include_path = '.; c: \ php5 \ pear ') in C: \ home \ website \ test. php on line 5 Hello World! Please note that the echo statement is still executed! This is because the warning will not stop the script execution. Now, let's use the require () function to run the same example.

PHP code:

 

Error message:

Warning: require(wrongFile.php) [function.require]:failed to open stream:No such file or directory in C:\home\website\test.php on line 5Fatal error: require() [function.require]:Failed opening required 'wrongFile.php'(include_path='.;C:\php5\pear')in C:\home\website\test.php on line 5

Because the script execution is terminated after a fatal error occurs, the echo statement is not executed.

Because the script is not executed after the file does not exist or is renamed, we recommend that you use require () instead of include ().

The above is the PHP advanced tutorial (2): the content of the PHP Reference File. For more information, see PHP Chinese network (www.php1.cn )!

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.