0

EDIT: Sorry I didn't make this more clear. Replace "Echo" with "return" in front of "hi" and it returns null. I'm just showing that I can get inside that conditional.

This PHP function is supposed to calculate a sum of values passed from a database into an array. The function needs to be able to accept multiple columns (array) of values, and return an array of their summed values.

Below: $val is the column name and $result is the data returned from the query. So the data needs to be matched up with the correct keys (help by $val). As you can see I tried adding some checks to make sure the function is returning at the correct time. Echoing the variable shows that I am reaching that point, ("hi" is echoed) but it always returns NULL. Please feel free to ask questions if this is not clear enough.

function getSum($val,$result,$i=1,$count)
{
    $sum = array();
    if(is_array($val) == true)
    {
        foreach($val as $henh)
        {
            //echo $i;
            $this->getSum($henh,$result,$i,$count);
            $i++;
        }
    }
    else
    {
        $strs = explode('.',$val);
        $str = $this->getParameters(array($strs[1]));
        //var_dump($str[$strs[1]]); exit;
        if($str[$strs[1]]['Parameter']['type']=='int' || $str[$strs[1]]['Parameter']['type']=='float')
        {
            //echo $i;
            $c = true;
            foreach($result as $item)
            {
                foreach($item as $k=>$v)
                {
                    if($k==$val AND $v !== null)
                    {
                        $c = true;
                        $sum[$val][] =  intval($v);
                    }
                }
            }
            if($i==$count AND isset($sum[$val]) !== 0)
            {
                echo "hi!";
            }
        }
    }
}
user3840170
  • 26,597
  • 4
  • 30
  • 62
Axschech
  • 297
  • 1
  • 2
  • 14

3 Answers3

2

There is no return statement in the function so it will not return anything, ever.

Harri
  • 2,692
  • 2
  • 21
  • 25
  • Your example is missing last curly bracket, it is calling functions we know nothing about and it is still missing the return statement. And `isset($sum[$val]) !== 0` makes no sense at all because it will always be true since you are comapring wheter true or false (return values from isset()) is not the same as 0. – Harri Feb 11 '14 at 18:01
  • It's isset($sum... AND $sum[$val] !== 0... no, $sum[$val] is not always set, that's what the loop inside the second half of the function does. All that one function does is pull in the datatype based off the name of the column in order to check it. I thought that was obvious. Maybe I'm not understanding what you are trying to say? – Axschech Feb 11 '14 at 18:10
  • By far the biggest problem is that you dont have return statement anywhere in the function. Way smaller problem is the isset() thing. But about that smaller issue, `isset($sum[$val])` will always return `true` or `false`. Then you compare that are `true` or `false` different than `0`. Since true or false are not same as 0, the whole comparison is always true, so it does not do anything. – Harri Feb 11 '14 at 18:15
  • did you look at my edit? it's supposed to return "hi" instead of echo it – Axschech Feb 11 '14 at 18:20
  • Yes I've read your edits but please keep the code example current. Since your example contains really basic mistakes, it is better that we don't guess what you have tried, but you actually provide the real example that you have tried. – Harri Feb 11 '14 at 18:22
  • I clearly pointed out where the problem was, especially after the edit. Any suggestions towards solving that problem would be appreciated! – Axschech Feb 11 '14 at 18:27
0

It is because you aren't returning anything. There is no return statement anywhere in your function: http://www.php.net/manual/en/function.return.php

TrippyD
  • 735
  • 5
  • 9
0

If you read the note on the PHP documentation, your function will return NULL if no return value is specified:

Note: If the return is omitted the value NULL will be returned.

So, a function, will in fact return. But it will return NULL.

http://www.php.net/manual/en/functions.returning-values.php

In addition to that, your syntax looks wrong for the if:

if($i==$count AND isset($sum[$val]) !== 0)

In PHP, the !== (triple inequality) operator will not use type coercion, so you are comparing the return value of isset() which returns type bool to 0. Also, AND is strange in PHP. It has a lower precedence than && and can lead to some weird output, if you're doing variable assignment, so I would recommend using && unless you intend to have lower precedence.

Your if should look something like:

if($i == $count && isset($sum[$val]) && $sum[$val] !== 0)

Lastly, you are calling your function recursively, but you are not returning the values recursively.

This:

 $this->getSum($henh,$result,$i,$count);

Should be like this:

 return $this->getSum($henh,$result,$i,$count);
Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109