Php 简明教程
PHP - Call by Value
默认情况下,PHP 对传递给函数的参数使用“按值调用”机制。当一个函数被调用时,实际实参的值会被复制到函数定义的形式实参中。
By default, PHP uses the "call by value" mechanism for passing arguments to a function. When a function is called, the values of actual arguments are copied to the formal arguments of the function’s definition.
在函数体执行期间,如果任何 formal arguments 的值发生任何变化,它不会反映在 actual arguments 中。
During the execution of the function body, if there is any change in the value of any of the formal arguments, it is not reflected in the actual arguments.
-
Actual Arguments − The arguments that are passed in a function call.
-
Formal Arguments − The arguments that are declared in a function definition.
Example
让我们考虑以下代码中使用的函数−
Let us consider the function used in the code below −
<?php
function change_name($nm) {
echo "Initially the name is $nm \n";
$nm = $nm."_new";
echo "This function changes the name to $nm \n";
}
$name = "John";
echo "My name is $name \n";
change_name($name);
echo "My name is still $name";
?>
它将生成以下 output −
It will produce the following output −
My name is John
Initially the name is John
This function changes the name to John_new
My name is still John
在这个示例中, change_name() 函数将 _new 附加到它传入的字符串参数。然而,传入它的变量的值在函数执行之后仍然保持不变。
In this example, the change_name() function appends _new to the string argument passed to it. However, the value of the variable that was passed to it remains unchanged after the function’s execution.
事实上,形式参数充当函数的局部变量。此类变量只可以在其初始化的范围内访问。对于函数而言,用花括号 "{ }" 标记的主体就是其范围。此范围内任何变量都不可用于其外部的代码。因此,任何局部变量的处理都不会影响外部的世界。
Formal arguments, in fact, behave as local variables for the function. Such a variable is accessible only inside the scope in which it is initialized. For a function, its body marked by the curly brackets "{ }" is its scope. Any variable inside this scope is not available for the code outside it. Hence, manipulation of any local variable has no effect on the world outside.
“按值调用”方法适合使用传给它的值来执行计算的函数。它执行某些计算并返回结果,无需改变传入它的参数的值。
The "call by value" method is suitable for a function that uses the values passed to it. It performs certain computation and returns the result without having to change the value of parameters passed to it.
Note − 执行公式类型计算的任何函数都是按值调用的示例。
Note − Any function that performs a formula-type computation is an example of call by value.
Example
请看以下示例:
Take a look at the following example −
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$x = 10;
$y = 20;
$num = addFunction($x, $y);
echo "Sum of the two numbers is : $num";
?>
它将生成以下 output −
It will produce the following output −
Sum of the two numbers is : 30
Example
下面是通过按值传递参数来调用函数的另一个示例。该函数将接收到的数字增加 1,但这不会影响传入它的变量。
Here is another example of calling a function by passing the argument by value. The function increments the received number by 1, but that doesn’t affect the variable passed to it.
<?php
function increment($num) {
echo "The initial value: $num \n";
$num++;
echo "This function increments the number by 1 to $num \n";
}
$x = 10;
increment($x);
echo "Number has not changed: $x";
?>
它将生成以下 output −
It will produce the following output −
The initial value: 10
This function increments the number by 1 to 11
Number has not changed: 10
PHP 也支持在调用时将变量的引用传递给函数。我们将在下一章讨论它。
PHP also supports passing the reference of variables to the function while calling. We shall discuss it in the next chapter.