在php中,通过引用调用的情况下,如果在函数内修改实际值,则修改了变量的值。 在这种情况下,需要使用&
(和号)符号与形式参数。 &
表示变量的引用。
让我们通过下面的例子来帮助理解引用调用的概念。
在这个例子中,变量$str
被传递给加法器函数,它与“call by reference
”字符串连接。 在这里,打印$str
变量的结果值为:’this is call by reference
‘。 这是因为改变的是变量$str
的实际值。
<?php
function adder(&$str2)
{
$str2 .= 'call by reference';
}
$str = 'this is ';
adder($str);
echo $str;
?>
输出结果如下 -
this is call by reference
让我们通过另一个例子来理解php中引用调用的概念。
<?php
function increment(&$i)
{
$i++;
}
$i = 10;
increment($i);
echo $i;
?>
输出结果如下 -
11