dexdyne Posted September 24, 2014 Share Posted September 24, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291250-testing-the-value-of-a-variable-holding-an-array/ Share on other sites More sharing options...
dexdyne Posted September 24, 2014 Author Share Posted September 24, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/291250-testing-the-value-of-a-variable-holding-an-array/#findComment-1491940 Share on other sites More sharing options...
dexdyne Posted September 24, 2014 Author Share Posted September 24, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/291250-testing-the-value-of-a-variable-holding-an-array/#findComment-1491942 Share on other sites More sharing options...
mac_gyver Posted September 24, 2014 Share Posted September 24, 2014 for both the code examples you have posted, testing if($var), that if() statement is false for an empty array. see this link for how php treates/compares values - http://php.net/manual/en/types.comparisons.php Quote Link to comment https://forums.phpfreaks.com/topic/291250-testing-the-value-of-a-variable-holding-an-array/#findComment-1491943 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.