Fastest/shortest way to check if value is numeric or false?
Posted: Tue Jan 05, 2010 9:52 am
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):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:Which works ok, but I am wondering if this slows things down.
The second one I've come up with is: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.: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
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();I have two possible fixes for this. The first is:
Code: Select all
is_numeric($bar = foo()) or die();The second one I've come up with is:
Code: Select all
($bar = foo()) !== false or die();Code: Select all
($bar = foo()) === true or die();Or do you perhaps know a better way to do this?
Thanks in advance,
Wouther