What is a custom function library?
Function library is not a PHP syntax for defining functions, but a design pattern in programming. Functions are modules of structured programming and are the core of code reuse. In order to better organize the code, customized functions can be used in multiple files in the same project. Multiple customized functions are usually organized into the same file or multiple files. These files that collect function definitions are used to create PHP function libraries. If you want to use the functions defined in these files in a PHP script, you need to use one of the functions include(), include_once(), require() and require_once() to load the function library file into the script in process.
php uses a custom function library
include() and require() introduction
The performance of the require() statement Like include(), both include and run the specified file. The difference is that with the include() statement, the file is read and evaluated each time it is executed; with the require() statement, the file is only processed once (in effect, the file content replaces the require() ) statement). This means that if there is code that is likely to be executed multiple times, use the include() statement.
The require() statement is used like require(" file.php "). This statement is usually placed as part of the php script file. The use of the include() statement is the same as the require() statement, such as include(" file.php "). This statement is generally placed in the processing section of flow control. The php script file reads the files it contains after reading the require() statement. In this way, the process of program execution can be simplified.
The code example is as follows:
<?php require "config.php"; //使用 require 语句包含执行 config.php文件 if($condition){ include "file.txt"; //使用include 语句包含并执行 file.txt文件 }else{ include ("other.php"); // 使用include 语句包含并执行 other.php文件 } require("somefile.txt"); // 使用require 语句包含执行 somefile.php文件 ?>
In the above example, include() and require() are used in a script file, include() When statements are placed in flow control processing, such as echo(), you can use the echo("abc") form, or you can use the echo "abc" form to output the string abc. The include() and require() statements can also add parameters directly without parentheses. For example, the include statement can use include("file.php") to include the file.php file. You can also use include "file.php" form.
include_once() and require_once() statements also include and run the specified file during script execution. This behavior is similar to include() and require() statements, and its usage is the same. The only difference is if the code in the file is already included. will not be included again. These two statements should be used when the same file may be included more than once during script execution to ensure that it is included once to avoid problems such as function redefinition and reassignment.
The above is an introduction to the four statements of how to use custom function libraries in PHP. In the next chapter, we will introduce in detail the difference between include() and require() statements. .
【Recommended related tutorials】
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2 . Video tutorial: Function import and file inclusion: use of include and require
3. php practical video tutorial
The above is the detailed content of How to use custom function library in php. For more information, please follow other related articles on the PHP Chinese website!