During the development process, it is often necessary to repeat certain operations and enter the same code repeatedly, which will increase the redundancy of the code and make it more troublesome to maintain later. If you use functions, these problems will be easily solved.
1. Define and call functions
Functions are to write some repeatedly used functions in an independent code block and call them separately when needed. The basic syntax for creating a function is:
function fun_name($str1, $str2...$strn) {
fun_body
}
Among them, function is a keyword that must be used when declaring a custom function. fun_name is the name of the custom function. $str1, $str2...$strn are the parameters of the function. fun_body is the body of the custom function and the code part that implements the function.
For example, the following function calculates the square of a number:
<?php //声明自定义函数 function example($num) { retrun $num * $num; //返回计算后的结果 } echo example(10); //调用函数 ?>
The result is: 100
2. Pass functions between functions
When calling a function, you need to pass parameters to the function, and the parameters that are passed in They are called actual parameters, while the parameters defined by the function are formal parameters. Parameter passing methods include pass by value, pass by reference and default parameters.
2.1 Pass by value
Pass by value is to copy the value of the actual parameter to the corresponding formal parameter. The operation pointer inside the function operates on the formal parameter, and the operation result will not affect the actual parameter. That is, after the function returns, the value of the actual parameter will not change.
Example:
<?php //定义一个自定义函数 function sp($m) { $m = 100; //输出形参 echo $m . "/; } $m = 5; sp($m); //传递值,将$m的值传递给形参$m echo $m; //输出实参 ?>
Run result: 100/5.
2.2 Pass by reference
Pass by reference is to pass the memory address of the actual parameter to the formal parameter. At this time, the operations inside the function will affect the actual parameters, and the values of the actual parameters will change after the function returns. To pass by reference, just add the & sign when passing the value.
For example:
<?php //定义一个自定义函数 function sp(&$m) { $m = 100; //输出形参 echo $m . "/"; } $m = 5; sp($m); //传递值 echo $m; //输出实参 ?>
Run result: 100/100.
2.3 Default parameters
Default parameters refer to the default values specified by the formal parameters.
Example:
<?php //定义一个自定义函数 function sp($m = 100) { //输出形参 echo $m . "/"; } sp(10); //传值 sp(); //未传值,输出默认值 ?>
Run result: 10/100.
3. Return value from function
Usually a function passes the return value to the caller by using the keyword return.
The return statement can only return one parameter, that is, it can only return one value, and cannot return multiple values at one time. If you need to return multiple values, you can use an array to store the return values in the array and return them.
The above is the content of PHP functions. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!