php function

不言
Release: 2023-03-24 16:54:01
Original
1392 people have browsed it

The content introduced in this article is about functions in php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Function
What is a function
A named block of code that completes a specified task and can be used repeatedly.
The role of function
Easy to maintain
The code is not redundant and can improve the reusability of the code
Types of functions
System functions
Custom functions
Steps to write custom functions
1. Write out the code you want to write
2. Wrap the code in braces
3. Use the keyword function () To declare the function
4. Extract the required or frequently changed value as a parameter
Note
1. The name of the function It must be meaningful
2. Function names cannot use pure numbers, and numbers cannot be used at the beginning
3. Function names are not case-sensitive
4. The name of the function cannot be repeated, nor can it be repeated with the custom one or the name of the system function
Call of the function
Note
1. The function will not be executed if it is not called.
2. The function call can be placed before the function declaration or after the function declaration. Because the code is placed in the code section of the memory, the code has been placed in the memory before the program is executed.
Parameters of custom functions
Since Define the design principle of the function. Users can participate in controlling the custom function and make some fine-tuning of the custom function
actual parameters
actual parameters when calling Parameters written in ()
Formal parameters
Formal parameters, when declaring parameters, provided parameters
Default parameters
If you do not fill in the specified number of actual parameters when using a function, an error will be reported, but we can give the formal parameters a default value while declaring the formal parameters
Note
1. When there is a default value in the formal parameter and there is a value in the actual parameter, the value in the actual parameter will overwrite the value of the formal parameter.
2. Yes Given some parameters, some parameters have default values, but they need to be placed as late as possible, because the parameter values are in one-to-one correspondence
3. Parameters that do not give default values in the function are called required parameters.
4. Parameters with default values in the function are called optional parameters
Variable length parameter list
func_get_args() Get the function The collection of all actual parameters
func_num_args() Gets the number of actual parameters in the function
func_get_arg (the number of parameters) Gets the value of the number of parameters
Note
1. Which parameter starts from 0 and not from 1
2. The actual parameter format in the function It won't go wrong if there are too many parameters, but they will be ignored by default. We can use system functions to obtain relevant information
Reference parameters
Application scenarios
Usually used when the parameter value itself needs to be modified Next
Note
1. When using reference parameters, & should be placed on the formal parameter
2. If it is a reference parameter, pass Variables must be used when parameters are used, and values cannot be used, because only variables have addresses
The return value in the custom function return
We sometimes need to perform the function on the result Process again, in order to solve this problem we need to use the return
format
return the value you want to return
Note
1. The return value of return will be returned wherever the function is called.
2. After the value returned by return is returned, the code after return will not be executed
3. Under normal circumstances, when we write a function, we will not use echo multiple times, but Is to return the value
Variables are classified according to scope
1. Local variables
Note
1. The variables declared by the function can only be called within the function. This is the local variable
2. The formal parameter of the function is actually a local variable. When calling, the actual parameters are passed to the inside of the function
2. Global variables
Variables declared outside the function are called global variables and can be used in the function (conditionally, you need to add the global keyword)
global variable name You can use external variables inside the function using the
global syntax
global Variable name 1, variable name 2 ,......
3. Super global variables
$GLOBALS refers to all variables in the global scope. This variable is automatically created. Contains all global variables, you need to use $GLOBALS['variable name'] to use
The difference between $GLOBALS and global
1. Outside the function The declared variables are called global variables and can be used in functions (conditionally, you need to add the global keyword)
2. $GLOBALS[] calls external variables and will remain inside and outside the function. consistent.
Static variable
Format
static variable name
Note
1. Static local variables will only be initialized once
2. Static properties can only be initialized to a character value or a constant or an expression
3 , will not change as the function is called and exits
Variable function
Run the value of the variable (string) as the name of the function plus parentheses The function

Copy after login

Callback function
The callback function actually passes the function name in the form of a string and then runs it using a variable function
Recursive function
Recursive function: call yourself
Note
1. You are this function, you are yourself Call yourself. When you finish something, you will do what you didn't finish last time.
2. The recursive function must have a termination condition, otherwise it will enter an infinite loop
File loading
include
Format
include('File path to include')
Note:
There are some functions and non-functions in the system. They are system commands. You can also write include ""
include_once
Include the file only once. It will check whether it is already included. If it is included, it will not include it again. If it is not included, it will only include it.
require
require('File path to be included')
require_once
also checks whether it is included. If it is included, it will no longer be included
Note
Note: include and require have similar functions, but they are not related to aliases
The difference between include and require
require: If an error is included, a fatal error will be generated
include: If an error is included, a warning will be generated, and the following code will continue to execute
Special attention
If you are missing the file you want to include and the program cannot run, use require, otherwise use include
What is a function
The named code block that completes the specified task can be used repeatedly.
The role of functions
Easy to maintain
The code is not redundant and can improve the reusability of the code
Types of functions
System function
Custom function
Steps to write a custom function
1. Write out the code you want to write
2. Wrap the code in curly brackets
3. Use the keyword function function name () to declare the function
4. Extract the required or frequently required values and use them as parameters
Note
1. The name of the function must be meaningful
2. Function names cannot use pure numbers, and numbers cannot be used at the beginning
3. Function names are not case-sensitive
4. Function names cannot be repeated. It cannot be repeated with the name of the customized one or the name of the system function
Call of the function
Note
1. If the function is not called, no Execution
2. The function call can be placed before the function declaration or after the function declaration. Because the code is placed in the code section of the memory, the code has been placed in the memory before the program is executed.
Parameters of custom functions
Since Define the design principle of the function. Users can participate in controlling the custom function and make some fine-tuning of the custom function
actual parameters
actual parameters when calling Parameters written in ()
Formal parameters
Formal parameters, when declaring parameters, provided parameters
Default parameters
If you do not fill in the specified number of actual parameters when using a function, an error will be reported, but we can give the formal parameters a default value while declaring the formal parameters
Note
1. When there is a default value in the formal parameter and there is a value in the actual parameter, the value in the actual parameter will overwrite the value of the formal parameter.
2. It can be given Some parameters have default values, but they need to be placed as late as possible, because the parameter values are in one-to-one correspondence
3. Parameters that do not give default values in the function are called required parameters
4. Parameters with default values in the function are called optional parameters
Variable length parameter list
func_get_args() Gets all the actual parameters in the function Set of parameters
func_num_args() Get the number of actual parameters in the function
func_get_arg (the number of parameters) Get the value of the number of parameters
Note
1. Which parameter starts from 0, not 1
2. There are more actual parameters than formal parameters in the function It won't go wrong if the number is large, but it will be ignored by default. We can use system functions to obtain relevant information
Reference parameters
Application scenarios
Usually used when the parameter value itself needs to be modified Next
Note
1. When using reference parameters, & should be placed on the formal parameter
2. If it is a reference parameter, pass Variables must be used when parameters are used, and values cannot be used, because only variables have addresses
The return value in the custom function return
We sometimes need to perform the function on the result Process again, in order to solve this problem we need to use the return
format
return the value you want to return
Note
1. The return value of return will be returned wherever the function is called.
2. After the value returned by return is returned, the code after return will not be executed.
3. Under normal circumstances, when we write a function, we will not use echo multiple times, but return the value
Variables are classified according to scope
1. Local variables
Note
1. Variables declared by a function can only be called within the function. This is a local variable
2. The formal parameter of a function is actually a local variable. When calling, the actual parameters are passed to the inside of the function
2. The global variable
is declared outside the function Variables are called global variables and can be used in functions (conditionally, you need to add the global keyword)
global variable name External variables can be used inside the function
Global concatenation method
global Variable name 1, variable name 2,......
3, super global variable
$GLOBALS refers to all variables in the global scope. This variable is automatically created. It contains all global variables. You need to use $GLOBALS['variable name'] to use it
$The difference between GLOBALS and global
1. Variables declared outside the function are called global variables and can be used in the function (conditionally, you need to add the global keyword)
2. $GLOBALS[] calls external variables and will remain consistent inside and outside the function.
Static variable
Format
static variable name
Note
1. Static local variables will only be initialized once
2. Static properties can only be initialized to a character value or a constant or an expression
3 , will not change as the function is called and exits
Variable function
Run the value of the variable (string) as the name of the function plus parentheses The function

Copy after login

回调函数
回调函数实际上就是将函数名以字符串的形式传递然后使用变量函数的方式来运行的
递归函数
递归函数:自己调用自己
注意
1、你自己就是这个函数,是你自己调用自己,当自己做完一件事情后,你会做上次你没做完的事情
2、递归函数一定要有一个终止条件,否则将进行死循环
文件加载
include
格式
include('要包含的文件路径')
注意:
系统里面有些函数非函数,它是系统命令你还可以写成 include “”
include_once
只包含一次该文件,他会检查是否已经包含了,如果包含了他就不再次包含,如果没有包含他才会包含
require
require('要包含的文件路径')
require_once
也是检查是否包含,如果包含了就不再包含
注意
说明: include和require他俩的功能差不多,但不是别名的关系
include和require的区别
require:如果包含错误的话,将会产生一个致命的错误
include:如果包含错误的话将会产生一个警告,下面的代码还会继续执行
特别注意
如果,你缺少了你要包含的文件程序运行不下去的时候使用require,反之使用include

相关推荐:

探究PHP的函数运行机制_PHP教程

The above is the detailed content of php function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!