Difference: 1. The formal parameters are the parameters in brackets after the function name when defining the function, and the actual parameters are the parameters in brackets after the function name when calling the function; 2. The formal parameters have no actual Meaningful parameters, and actual parameters are parameters with actual data meaning.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
The parameters of the function are divided into two types: formal parameters and actual parameters
Formal parameters: Formal parameters, parameters that have no actual meaning, are the parameters used when defining the function declaration
actual parameters Parameters: Actual parameters, parameters with actual data meaning, are parameters used when calling functions
1. Formal parameters
Formal parameters It is the parameter list in parentheses after the function name when defining a function (referred to as "formal parameters"). Just like its name, the formal parameters themselves have no specific values. Because the function body needs to use external parameters, in order for the parameters to be passed in correctly, it needs to be passed through the formal parameters and the data in the function body. The formal parameters are as shown in the figure below.
[Example] The formal parameters of a function are as follows:
<?php function hello($str){ echo '参数 $str 的值为:'.$str.'<br>'; echo 'php中文网'; } ?>
Among them, the variable $str in parentheses after the function name in line 2 of the code is The formal parameters of this function.
2. Actual parameters
The actual parameters are the several parameters in parentheses after the function name when we call the function (referred to as "actual parameters"). The actual parameters and The formal parameters need to correspond one-to-one in order. It will replace the corresponding variable value of the formal parameter in the function body. The parameter of the function can be a specific value or a variable. The actual parameters are as shown in the figure below.
[Example] The following code demonstrates the actual parameters used when calling the function:
<?php function add($a, $b){ echo $a.' + '.$b.' = '.($a+$b).'<br>'; } add(11, 32); ?>
Among them, the function name in line 6 of the code is in parentheses. 11 and 32 are the actual parameters.
Note: The actual parameters must be the same as the number of formal parameters, and correspond one to one, otherwise the program will go wrong.
[Example] When calling a function, when the number of actual parameters is different from the number of formal parameters, a fatal error will occur:
<?php function add($a, $b){ echo $a.' + '.$b.' = '.($a+$b).'<br>'; } add(11); ?>
The running structure is as follows:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function add().
Recommended learning : "PHP Video Tutorial"
The above is the detailed content of What is the difference between formal parameters and actual parameters in php. For more information, please follow other related articles on the PHP Chinese website!