troubleshooting Question

How can I dummy proof evaluating whether or not an incoming value is an integer or not?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
PHP
3 Comments1 Solution18 ViewsLast Modified:
I want to incorporate some validation in the code that's processing some incoming form data. I need to make sure that a value coming in is either an integer or a decimal. Here's what I've got:

<?php
$number = 32,;

if(is_float($number)) {
      echo "good";
}
      elseif(is_int($number)) {
            echo "good";
      }
else {
      echo "nope";
}

?>

This will work for either a regular number of a decimal, but...

...if the user makes the mistake of including a "," or anything bogus, rather than the script returning "nope," I get an error that says, "arse error: syntax error, unexpected ',' in C:\wamp64\www\adm\jquery.php on line 8." Line #8 being $number = 32,;

How can I trim or qualify the incoming value so if there's something ridiculous coming in, it won't even try to evaluate it? In other words, what can I do before I even attempt to discern whether the value is an integer to ensure that I'm not going to get an error before I even start?

Thanks!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros