日期:2014-05-17  浏览次数:20590 次

PHP代码的输出结果
<?php

  $sum=10;
  function demo($number){
  $number=$number+20;
  }
  demo(&$num);
  echo $num,"\n";
   
?>
结果为什么是20,而不是30?

------解决方案--------------------

你这个不对,结果是10,$num是全局变量

应该改为

$num=10;
function demo($number)
{
$number=$number+20;
return $number;
}
$num = demo($num);
echo $num,"\n";

这样结果才是30

探讨

PHP code

/*
Deprecated: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of demo(). If you would like to enable call-time pass-by-r……

------解决方案--------------------
$num=10;
function demo(&$number){
$number=$number+20;
}
demo($num);
echo $num,"\n";

如果是这个意思,那么结果是30