Php 简明教程
PHP - Function Parameters
PHP 中的某个函数可能会被定义为接受一个或多个参数。在定义函数时,函数参数是函数名称前面圆括号内的表达式的逗号分隔列表。参数可以是任何标量类型(数字、字符串或布尔值)、数组、对象,甚至可以是另一个函数。
A function in PHP may be defined to accept one or more parameters. Function parameters are a comma-separated list of expressions inside the parenthesis in front of the function name while defining a function. A parameter may be of any scalar type (number, string or Boolean), an array, an object, or even another function.
function foo($arg_1, $arg_2, $arg_n) {
statements;
return $retval;
}
在函数体中,参数作为要处理的变量。因此,它们遵循与任何普通变量相同的命名约定,即它们应以 “$” 开头,可以包含字母、数字和下划线。
The arguments act as the variables to be processed inside the function body. Hence, they follow the same naming conventions as any normal variable, i.e., they should start with "$" and can contain alphabets, digits and underscore.
Note −定义的参数数量没有限制。
Note − There is no restriction on how many parameters can be defined.
当需要调用参数化的函数时,你必须确保传递给它的值与函数定义中的参数数量相同。
When a parameterized function needs to be called, you have to make sure that the same number of values as in the number of arguments in function’s definition, are passed to it.
foo(val1, val2, val_n);
使用参数定义的函数可以产生根据传递的值动态改变的结果。
A function defined with parameters can produce a result that changes dynamically depending on the passed values.
Example
以下代码包含一个带有两个参数的 addition() 函数定义,并显示这两个参数的和。运行时输出取决于传递给函数的两个值。
The following code contains the definition of addition() function with two parameters, and displays the addition of the two. The run-time output depends on the two values passed to the function.
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result";
}
addition(10, 20);
$x=100;
$y=200;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
First number: 100
Second number: 200
Addition: 300
Formal and Actual Arguments
有时, argument 一词被用来表示 parameter 。实际上,这两个术语之间存在一定的差异。
Sometimes the term argument is used for parameter. Actually, the two terms have a certain difference.
-
A parameter refers to the variable used in function’s definition, whereas an argument refers to the value passed to the function while calling.
-
An argument may be a literal, a variable or an expression
-
The parameters in a function definition are also often called as formal arguments, and what is passed is called actual arguments.
-
The names of formal arguments and actual arguments need not be same. The value of the actual argument is assigned to the corresponding formal argument, from left to right order.
-
The number of formal arguments defined in the function and the number of actual arguments passed should be same.
Example
当实际实参数量少于形式实参数量时,PHP 会引发 ArgumentCountError 。但是,如果实际实参比形式实参多,额外的实际实参会被忽略。
PHP raises an ArgumentCountError when the number of actual arguments is less than formal arguments. However, the additional actual arguments are ignored if they are more than the formal arguments.
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result \n";
}
# Actual arguments more than formal arguments
addition(10, 20, 30);
# Actual arguments fewer than formal arguments
$x=10;
$y=20;
addition($x);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments
to function addition(), 1 passed in /home/cg/root/20048/main.php
on line 16 and exactly 2 expected in /home/cg/root/20048/main.php:2
Arguments Type Mismatch
PHP 是一种动态类型语言,因此它在使用实际实参的值复制形式实参时不强制进行类型检查。然而,如果函数体内的任何语句尝试对不支持特定数据类型执行特定操作,PHP 会引发异常。
PHP is a dynamically typed language, hence it doesn’t enforce type checking when copying the value of an actual argument with a formal argument. However, if any statement inside the function body tries to perform an operation specific to a particular data type which doesn’t support it, PHP raises an exception.
在上面的 addition() 函数中,假定传递的是数值实参。如果传递字符串实参,PHP 不会有任何反对意见,但执行加法的语句会遇到异常,因为字符串类型未定义“+”运算。
In the addition() function above, it is assumed that numeric arguments are passed. PHP doesn’t have any objection if string arguments are passed, but the statement performing the addition encounters exception because the "+" operation is not defined for string type.
Example
请看以下示例:
Take a look at the following example −
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result";
}
# Actual arguments are strings
$x="Hello";
$y="World";
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
PHP Fatal error: Uncaught TypeError: Unsupported operand types: string + string in hello.php:5
但是,PHP 是一种弱类型语言。它会尝试尽可能将变量强制转换为兼容类型。因此,如果传递的值之一是数字的字符串表示,而第二个是数值变量,则 PHP 会将字符串变量强制转换为数值类型来执行加法运算。
However, PHP is a weakly typed language. It attempts to cast the variables into compatible type as far as possible. Hence, if one of the values passed is a string representation of a number and the second is a numeric variable, then PHP casts the string variable to numeric in order to perform the addition operation.
Example
请看以下示例:
Take a look at the following example −
<?php
function addition($first, $second) {
$result = $first+$second;
echo "First number: $first \n";
echo "Second number: $second \n";
echo "Addition: $result";
}
# Actual arguments are strings
$x="10";
$y=20;
addition($x, $y);
?>
它将生成以下 output −
It will produce the following output −
First number: 10
Second number: 20
Addition: 30