Front-end PHP primer -018-built-in function file containing function

Source: Internet
Author: User

In real-world development, it is often necessary to put the common code in the program into a file, the files that use the code only need to include this file. This method can help to improve the reusability of code, which brings great convenience to the writing and maintenance of code.
In PHP, there are four methods of require, require_once, include, and include-once that contain a single file.

function contains failed features
Inlcude Returns a warning The file continues to execute downward. Typically used to dynamically include
Require A fatal mistake. The code will not continue to execute down. Usually contains extremely important files, the entire code does not want to execute
Include_once Returns a warning In addition to the functionality of the original include, it will also do once detection, if the file has been included, no longer contain
Require_once A fatal mistake. In addition to the original function one, will do a once detection, to prevent the file is repeatedly contained

Attention:

    1. Use less _once with once because it consumes more resources to do the work of testing.
    2. The extra advanced include file only needs to be compiled once, because each include include will execute the corresponding code once, how to reduce the need to re-parse when the include is executed again.

* * 1.include contains function functions. **
Create a functions.php file with two functions written in it:

  
 
  1. <?php
  2. //functions.php文件
  3. function demo(){
  4. echo ‘aaaa‘;
  5. }
  6. function test(){
  7. echo ‘cccdddd‘;
  8. }
  9. ?>

In functions.php's sibling directory, I create a user.php file to include the functions.php file. So my function can be put in functions.php, where I need to use these functions, where I include them:

  
 
  1. <?php
  2. //user.php
  3. include ‘functions.php‘;
  4. //可以直接调用
  5. demo();
  6. test();
  7. ?>

I passed the example above and we know the features of the include. Next we compare the include and require:

In the code, we first use include to include the nonexistent test.php file

  
 
    1. //user.p HP
    2. include ' functions.php '
    3. include ' test.php ' Span class= "pun";
    4. /span>//can directly call
    5. demo
    6. T EST ();

Then use require to include test.php files that do not exist:

  
 
  1. <?php
  2. //user.php
  3. include ‘functions.php‘;
  4. require ‘test.php‘;
  5. //可以直接调用
  6. demo();
  7. test();
  8. ?>

By comparing the above example, we find that:

    • If the test.php file does not exist, include warns you to continue with the demo () and test () functions.

    • Requre directly error, the demo () and test () function can not continue to execute.



From for notes (Wiz)

Front-end PHP primer -018-built-in function file containing function

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.