Forum Moderators: coopster

Message Too Old, No Replies

Which method is best to verify contents of a variable.

preg_match() or is_int()

         

endtheme

8:31 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



A script gets a variable,
$_GET['id']
, through the URL, such as,
?id=3523415

Which is the best way to verify that the variable only contains numbers? Would it be:

a.

preg_match("~^[0-9]+$~",$_GET['id'])

or

b.

is_int($_GET['id'])

endtheme

8:32 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



Hmm, it just occurred to me that integers can be negative, therefore a value such as,
-30
would return true in scenario b, and false in a. I suspect I've answered my own question then.

rob7591

8:44 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



If you only want it to be positive numbers you can do
if ($_GET['id'] > 0 && is_int($_GET['id'])

regexp can slow you down sometimes..

RonPK

4:53 pm on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_GET variables are always strings, so testing them with is_int() will always return false.