How to use a custom function library in PHP

Source: Internet
Author: User
What is a custom function library?

A function library is not a PHP syntax for defining functions, but rather a design pattern when programming. function is the module of structured programming, which is the core of implementing code reuse. To better organize your code, a custom function can be used in multiple files of the same project, typically organizing multiple custom functions into the same file or multiple files. The files defined by these collection functions are the creation of PHP function libraries. If you want to use the functions defined in these files in a PHP script, you need to load the library file into the script using one of the functions in include (), include_once (), require (), and require_once () .

PHP uses a custom function library

Introduction to include () and require ()

The performance of the Require () statement and include () includes and runs the specified file. The difference is that, for the include () statement, it is read and evaluated every time the file is executed, whereas for the require () statement, the file is processed only once (in fact, the file contents replace the Require () statement). This means that the include () statement is used if it is possible to execute multiple times of code.

The Require () statement is used as a require ("file.php") statement, which is usually placed in a part of the PHP script file. The include () statement is used in the same way as the require () statement, such as include ("file.php"). This statement is generally placed in the processing section of the Process Control. The php script file reads the require () statement before the file it contains is read in. In this way, the process of executing the program can be simplified.

Its code example is as follows:

<?php require "config.php";      Use the Require statement to include the Execute config.php file if ($condition) {   include "file.txt";      Use the include statement to include and execute the file.txt file}else{   include ("other.php");   Use the include statement to include and execute the other.php file} require ("Somefile.txt");   Use the Require statement to include the Execute somefile.php file?>

The previous example uses the include () and require ( ) two statements in a script file, where the include () statement is placed in process-controlled processing, such as echo () can use Echo ("abc"), or echo "ABC" can be used The form output string ABC. The include () and require () statements can also add arguments without parentheses, such as the include statement can use include ("file.php") to include file.php files, or you can use the include "file.php" Form.

The include_once () and require_once () statements also include and run the specified file during script execution. This behavior is similar to the include () and require () statements, as are the methods used. The only difference is if the code in the file has already been included. is not included again. These two statements should be used in cases where the same file may be included more than once during script execution to ensure that it is included once to avoid problems such as redefinition and re-assignment of functions.

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.