Introduce PHP inclusion file reuse PHP program code _ PHP Tutorial

Source: Internet
Author: User
Introduce PHP inclusion files to reuse PHP program code. When the same content is repeatedly used on the page, use the include () function to include files. this method can simplify coding. For example, to associate a database with the same content, use the include () function to include files when the same content is repeatedly used on the page header or page. this method can simplify coding. For example, associate with the same database, and display the same HTML in the header or footer of the page. There are countless similar applications. So how to apply it? Now, in the "example. inc" file, add the following code: Just like writing all of them directly to the previous code. The only thing to note is that you must add the PHP separator (the first and last lines of the above code) to the included file. Otherwise, the code will only be displayed and not executed. In fact, you can specify the names of any contained files. the. inc file name is used to separate them from other PHP scripts. You can also implement it using the following method, What is the difference between this and the include () function above? If the file contained by the require () function does not exist, the program stops running when the script is run and an error message is displayed. If include is used, a warning is displayed and the script continues to be executed. In the following example, an error may occur. When using the include () function to include data, the following error occurs: Warning: Failed opening hello. inc for future Sion (include_path =) in/tutorial/9.php on line 6 if you use the require () function to contain files, the following error message may appear: Fatal error: Failed opening required hello. inc (include_path =) in/tutorial/9.php on line 6 see the nuances in it? If you use require to include a file, a "severe error is given, while using the include () function to include the file only gives a" warning ". The only problem with containing files is whether the password is saved in the include file. Since PostgreSQL does not need a password when connecting to the database (at least in this example), this issue is not important. In future examples, we will use a file to include all the information. [Supplement -- by Boban] 1). The difference between the two is that the content contained in the require () function is a component of the entire document, while the include () function is not. In a correct PHP program, if the require () function is used, the PHP script of the entire document will not run if the file syntax or other errors are included; if the include () function is used, the program prompts an error only when there is a syntax error, and other correct parts of the PHP script continue to run. 2 ). the include () function can be used in condition statements and loop control statements, such as if (1 = 1) to determine whether the file needs to be included, while require () the function will ignore these conditions, regardless of 3721. 3) if the include file defines variables, functions, classes, etc, it is best to use the include () function and make a macro definition, such as if (! Define ("_ EXAMPLE_INC") define ("_ EXAMPLE_INC", 1); the statement prevents errors caused by repeated inclusion.

The extract () function contains files. this method can simplify coding. For example, associate with the same database; in the page header or page...

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.