In PHP functions, formal parameters are placeholders when the function is defined, representing the data to be received, and actual parameters are the actual data passed to the formal parameters when the function is called. Formal parameters can specify types and default values, and actual parameters can be passed positionally or by name.
Formal parameters and actual parameters of PHP functions: in-depth understanding
Formal parameters and actual parameters
In PHP functions, formal parameters (parameters) are placeholders specified when defining the function and are used to receive the data actually passed to the function. Argument is the data passed to the formal parameter when the function is actually called.
Types and default values of formal parameters
Formal parameters can specify a type (such as integer, string) or a default value. If no default value is specified, the variable's type is mixed
, which means it can receive any type of data. For example:
function add($num1, $num2 = 0) { // ... }
Passing actual parameters
Parameters can be passed to functions by position or by name:
For example:
$result = add(10, 20); // 按位置传递 $result = add(num2: 20, num1: 10); // 按名称传递
Practical case: Calculate the area of a circle
<?php function calculateAreaCircle($radius) { return pi() * $radius ** 2; } $radius = 5; $area = calculateAreaCircle($radius); echo "圆的面积为:$area"; ?>
Summary
The above is the detailed content of What is the difference between formal parameters and actual parameters of a PHP function?. For more information, please follow other related articles on the PHP Chinese website!