Jump to content
MakeWebGames

Check Int?


Dillion & Amanda 4 Lif

Recommended Posts

function check_int($check){
if(preg_match("#\D#is", $check)){
 echo 'Error text here.';
 exit;
}

if(!ctype_digit($check) || !is_numeric($check)){
 echo 'Error text here.';
 exit;
}
}

 

Just thought of it. Basically first it checks if it matches anything except a number. Then it checks if its a valid number. Please tell me if its useful or something, not sure but it works.

Link to comment
Share on other sites

I see your logic.

But not really for mccodes. In order for it to work you would have to implement it through the code and if your doing that just change the code to make sure its checking correctly anyway.

Also its my understanding. ctype_digit will return false if its not a number anyway.

Link to comment
Share on other sites

If you take your code and put it into pseudo-code, here's what it is:

function check_int:

if (not a number) exit;

if (not a number or not a number) exit;

You're using a lot of redundant code. There is no need for both conditionals, both perform (almost) the same tests.

In your second conditional, if the variable passes the ctype_digit test, then it will pass the is_numeric test for sure (since ctype_digit is a stricter test than is_numeric), making the second part of the if also redundant.

 

Your function will give the same results as this:

function check_int($check){
if(!ctype_digit($check) ){
 echo 'Error text here.';
 exit;
}
}

 

Also, you don't want to be using exit in a function, use return, otherwise you are breaking the flow of the page.

Link to comment
Share on other sites

Well if u add $h->endpage() above exit for mccodes itll keep the right design.

I think you missed the point of my post.

But that's still bad coding practice. Why would you need to stop the script if a variable isn't an integer? Surely you would want to let the script handle the error, and display its own error message or handle it some other way. All your function does is check if a variable is an integer, in very few situations will you ever want the entire page to stop working just because a variable is a wrong type.

I would suggest returning true or false, if you extend the function in a useful way. At the moment it's just a slower alias of 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...