Reputation: 1
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. 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.
Upvotes: 0
Views: 35
Reputation: 1067
Your function returns null because you didn't return your array.
Modify your function like this :
function getDivisors($num) {
for ($i=1; $i<=$num; $i++) {
if ($num % $i == 0) {
$divisors[] = $i;
}
}
return $divisors;
}
Upvotes: 1