在php中按值调用和按引用调用之间的区别以及$$意味着什么? [英] difference between call by value and call by reference in php and also $$ means?
问题描述
(1)我想知道php
中的按值调用和按引用调用之间的区别是什么. PHP可以按值调用还是按引用调用?
(1) I want to know what is the difference between call by value and call by reference in php
. PHP works on call by value or call by reference?
(2)我也想知道您是通过$$登录php
(2) And also i want to know that do you mean by $$ sign in php
例如:-
$a = 'name';
$$a = "Paul";
echo $name;
output is Paul
如上例所示,PHP上的$$是什么意思.
As above example what do u mean by $$ on PHP.
推荐答案
$$a = b;
在PHP中的意思是取$a
的值,并将其 name 值为该值的变量设置为等于b
".
$$a = b;
in PHP means "take the value of $a
, and set the variable whose name is that value to equal b
".
换句话说:
$foo = "bar";
$$foo = "baz";
echo $bar; // outputs 'baz'
但是,是的,请阅读 PHP符号参考.
对于按值/引用进行调用-两者之间的主要区别在于您是否能够修改用于调用该函数的原始项.参见:
As for call by value/reference - the primary difference between the two is whether or not you're able to modify the original items that were used to call the function. See:
function increment_value($y) {
$y++;
echo $y;
}
function increment_reference(&$y) {
$y++;
echo $y;
}
$x = 1;
increment_value($x); // prints '2'
echo $x; // prints '1'
increment_reference($x); // prints '2'
echo $x; // prints '2'
请注意,increment_value()
不会更改$x
的值,而increment_reference()
会更改 .
Notice how the value of $x
isn't changed by increment_value()
, but is changed by increment_reference()
.
如此处所示,是使用按值调用还是按引用调用取决于被调用函数的定义.声明自己的函数时,默认值是按值调用(但您可以通过&
标记指定按引用调用).
As demonstrated here, whether call-by-value or call-by-reference is used depends on the definition of the function being called; the default when declaring your own functions is call-by-value (but you can specify call-by-reference via the &
sigil).
这篇关于在php中按值调用和按引用调用之间的区别以及$$意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!