Page 1 of 1

Fastest/shortest way to check if value is numeric or false?

Posted: Tue Jan 05, 2010 9:52 am
by Wouther
Hi all,

First of all, I got it to work, but am looking for advice on better ways to do it. (Perhaps one way is faster than another?) If this belongs in the Design section, I am sorry: can a moderator please move it then?

I have a function foo() that returns either an integer (greater than or equal to zero) or false on an error. I used the following line to call it, and die() on an error (returned false):

Code: Select all

$bar = foo() or die();
It works, except when foo() returns 0, in which case 0 is treated as false and it die()s. (This is correct according to the php website.)

I have two possible fixes for this. The first is:

Code: Select all

is_numeric($bar = foo()) or die();
Which works ok, but I am wondering if this slows things down. :?:

The second one I've come up with is:

Code: Select all

($bar = foo()) !== false or die();
This works, but I thought it looked rather silly, testing for it not to be equal to false. Sounded like testing for it to be true, e.g.:

Code: Select all

($bar = foo()) === true or die();
However, this does not work when foo() returns 0! Anyone know why this is different? :?:

Or do you perhaps know a better way to do this?

Thanks in advance,
Wouther

Re: Fastest/shortest way to check if value is numeric or false?

Posted: Tue Jan 05, 2010 11:05 am
by AbraCadaver
I would vote for this way. It makes sense and is normally how it is done in an if statement for example. Also, you are testing for a specific value, false, instead of things that may evaluate to true or false.
Wouther wrote:

Code: Select all

($bar = foo()) !== false or die();

Code: Select all

($bar = foo()) === true or die();
However, this does not work when foo() returns 0! Anyone know why this is different? :?:
Using === means that it must be boolean true, not an integer of any value. So the only time this won't die is when the function returns true. If you were to use == then you're back to the same problem because 0 would evaluate to false.

Re: Fastest/shortest way to check if value is numeric or false?

Posted: Tue Jan 05, 2010 11:14 am
by AbraCadaver
You might look at exceptions. Instead of returning false you can throw an exception and then catch it:

Code: Select all

function foo() {
    //do stuff to calculate $return
    if($return === false) {
        throw new Exception('Something is wrong!');
    }
    return $return;
}
 
try {
    $bar = foo();
} catch (Exception $e) {
   die($e->getMessage());
}

Re: Fastest/shortest way to check if value is numeric or false?

Posted: Tue Jan 05, 2010 11:45 am
by pickle
I'd do it this way - more lines, but it's easier to read:

Code: Select all

$bar = foo();
if($bar === FALSE)
  die();

Re: Fastest/shortest way to check if value is numeric or false?

Posted: Tue Jan 05, 2010 5:50 pm
by Jenk

Code: Select all

$bar = foo();
if (is_numeric($bar)) {
  // is numeric
} else {
 // is not
}
readability > preoptimisation.