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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Wouther
Forum Newbie
Posts: 1
Joined: Tue Jan 05, 2010 9:05 am

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

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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());
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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();
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

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

Post by Jenk »

Code: Select all

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