Jump to content

testing the value of a variable holding an array.


dexdyne

Recommended Posts

I have inherited some php code, but I am not a real php programmer.

 

I have just chased down a bug, and I'd like some advice on it.

 

The code is full of snippets like:

 

 

        $paramNum = ControlCentre::getElementIds(ControlCentre::PARAMETER, $bdb, 1, $filterType, $filterId);
 
        $numberOfDpText = $paramNum ? " - (".count($paramNum)." %datapoints)" : "";
 
Now getElementIds can return a NULL for failure, if the underlying sql reports one ( yes I know that's rubbish too ).
 
It is defined as returning an empty array if there are no elements to report on, but it turns out that it returns a NULL for that situation too.
I will eventually make it do what it says on the can, but there may/will be cascade effects on code like the above.
 
So -- it is clear that the code does if() on a variable which may hold an array, or may be NULL.
 
If this were C, then that would be a common, if mucky, method to ask
    if ( NULL == $paramNum )
but this ISN'T C.
 
So - do the panel agree that the code snippet above is just basically rubbish programming, or is there something subtle in PHP that I don't grok which makes it sensible and well-defined.
 
 
 

Just as a follow-up, and on the same lines...

 

it seems to me that the following code does in fact print "NO".

I'm not sure if it is defined to do so, or in fact prints "YES" or "NO" at random.

 

$x=array()

if ($x)

{

    echo "YES";

}else{

    echo "NO"

}

 

Does the answer to that question relate to, and/or help with the main question?

Oh, and another goody, just a bit further down..

 

 

        $smsTotal = array_pop($smsIds);
        if($smsId)
        {
            $smsId = intval($smsId);
            if($smsId < 0 || $smsId > $smsTotal)
                $smsId = NULL;
        }
 
if $smsIds is an empty array, $smsTotal will be NULL, and here we are doing 'greater than' on it.
 
I hope someone can assure me that NULL is arithmetically interchangeable with zero in PHP, or I've got a lot o f patches coming.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.