PHP 参数变量 vs 引用

php中的函数有什么区别

  • 通过变量传递参数
  • 通过引用传递参数?
卡洛·科卡克

按值传递参数- 传递变量的副本。

$b = 1;
function a($c) {
   $c = 2; // modifying this doesn't change the value of $b, since $c a copy of $b; thus a separate entity.
}

a($b);
echo $b; // still outputs 1

通过引用传递参数?- 传递指向函数外部变量的指针。

$b = 1;
function a($c) {
   $c = 2; // modifying this also changes the value of $b, the variable $c points to.
}

a($b);
echo $b; // outputs 2 now after calling a();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章