Val*_*lva 1 php logic function
<?php
function a($n){
return ( b($n) * $n);
}
function b(&$n){
++$n;
}
echo a(5);
?>
Run Code Online (Sandbox Code Playgroud)
我在星期天做了一个考试,想知道为什么这段代码的输出是0?
我不是PHP的开发人员所以任何帮助将不胜感激.
代码给出,0因为它缺少一个return.与下面的比较(如图所示进行校正)产生36,如另一个答案中所推断的那样.
function a($n){
// Since b($n) doesn't return a value in the original,
// then NULL * $n -> 0
return ( b($n) * $n);
}
function b(&$n){
// But if we return the value here then it will work
// (With the initial condition of $n==5, this returns 6 AND
// causes the $n variable, which was passed by-reference,
// to be assigned 6 such that in the caller
// it is 6 * $n -> 6 * 6 -> 36).
return ++$n;
}
echo a(5);
Run Code Online (Sandbox Code Playgroud)
见引用传递如何function b(&$n)上述作品; 如果签名是function b($n)结果将是30.