简体   繁体   English

PHP function output 中的一些错误我无法理解

[英]Some error in PHP function output I cannot understand

Have a 'function use' lesson problem.有一个“功能使用”课程问题。 The code inside the function (when it's put outside the function; returns an array) seems correct, but the function outputs NULL. Here's what it looks. function 中的代码(当它放在 function 之外时;返回一个数组)似乎是正确的,但 function 输出 NULL。这是它的样子。 Point where I'm doing wrong, please.请指出我做错的地方。

function getDivisors($num)
       {
         for ($i=1; $i<=$num; $i++)
           {
             if ($num % $i == 0)
               {
                 $divisors[] = $i;
               }
           }
        }

I suppose the output to the array is an incorrect, but... that's still unsure.我想数组的 output 是不正确的,但是......这仍然不确定。

Your function returns null because you didn't return your array.你的 function 返回null因为你没有返回你的数组。

Modify your function like this:像这样修改您的 function:

function getDivisors($num) {
  for ($i=1; $i<=$num; $i++) {
     if ($num % $i == 0) {
        $divisors[] = $i;
     }
  }
  return $divisors;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM