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.
 
 
 
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.