Avatar of Bruce Gust
Bruce Gust
Flag for United States of America asked on

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

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!
PHP

Avatar of undefined
Last Comment
Bruce Gust

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dave Baldwin

The error you're getting is a simple syntax error.  Form data always comes in as text.  To use it as a number you have to convert it.  I don't think PHP's automatic text to number conversion will work on '32,'.  ??
Bruce Gust

ASKER
Gentlemen, I've got to do some validation on some code that is not my own and it's kicking me in the butt!

If either of you have a moment, I'd love for you to see what works and what's not working so I can figure this thing out.

The question is at https://www.experts-exchange.com/questions/29183509/How-to-loop-through-this-posted-array-and-figure-out-what's-going-on.html 

Thanks!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23