Akash Posted January 10, 2009 Posted January 10, 2009 Hi guys, I just wanted to know which piece of code out of the following two is better to use. Basically, all I want is to makesure that the $_POST['hs'] from the form submitted is numerical. In one I have used eregi, and I don't actually know if it's right. The other uses is_numeric. Eregi if (!eregi('[0-9]', $_POST['hs'])) { echo'Invalid scores'; $h->endpage(); exit; } is_numeric if(!is_numeric($_POST['hs'])) { echo'Invalid scores'; $h->endpage(); exit; } Quote
Lithium Posted January 10, 2009 Posted January 10, 2009 Re: Which piece of code is better to use? they are "basicly the same", and you could also include a thrid one, ctype_digit(). though if you want to speed up code... ereg() ctype_digit() - faster than ereg() is_numeric() - faster than ctype_digit() Hope, this is what you wanted to know ;) Quote
POG1 Posted January 10, 2009 Posted January 10, 2009 Re: Which piece of code is better to use? ereg('[^0-9]',$subject) Quote
POG1 Posted January 10, 2009 Posted January 10, 2009 Re: Which piece of code is better to use? i changed my mind lol preg_match.. Quote
Akash Posted January 10, 2009 Author Posted January 10, 2009 Re: Which piece of code is better to use? So I'm guessing Lithium's right then, as no one's challenged him :P. Thanks for your help guys. Quote
POG1 Posted January 11, 2009 Posted January 11, 2009 Re: Which piece of code is better to use? That site shows some very interesting points... date('U'): 19.162 seconds time(): 0.057 seconds Time saved: 19.105 seconds; 99.7% Also it loooks like ctype_digit is the better choice; Regular Expressions: 2.401 seconds ctype_digit: 0.985 seconds Time saved: 1.416 seconds; 58.98% Quote
Lithium Posted January 11, 2009 Posted January 11, 2009 Re: Which piece of code is better to use? That site shows some very interesting points... date('U'): 19.162 seconds time(): 0.057 seconds Time saved: 19.105 seconds; 99.7% Also it loooks like ctype_digit is the better choice; Regular Expressions: 2.401 seconds ctype_digit: 0.985 seconds Time saved: 1.416 seconds; 58.98% true when ctype_digit() vs. ereg(), though benchmark it against is_numeric() and you'll see that is faster than ctype_digit() ;) Quote
Akash Posted January 11, 2009 Author Posted January 11, 2009 Re: Which piece of code is better to use? Thanks for all the help guys. :) Quote
Floydian Posted January 12, 2009 Posted January 12, 2009 Re: Which piece of code is better to use? Personally, I would recommend type casting first, and then doing a check. settype($blah, 'int'); Then check if $blah is less than 1 or 0 if you want to filter out negs. Quote
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.