当前位置:Gxlcms > PHP教程 > PHP代码的输出结果解决思路

PHP代码的输出结果解决思路

时间:2021-07-01 10:21:17 帮助过:16人阅读

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

人气教程排行