Jump to content
MakeWebGames

Which piece of code is better to use?


Akash

Recommended Posts

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; }
Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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%

Link to comment
Share on other sites

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() ;)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...