PHP Classes

php integer verification

Recommend this page to a friend!

      No Timeout  >  All threads  >  php integer verification  >  (Un) Subscribe thread alerts  
Subject:php integer verification
Summary:How do I verify if no_customers is an integer using PHP?
Messages:3
Author:steve
Date:2006-10-18 04:42:19
Update:2006-10-31 04:25:06
 

  1. php integer verification   Reply   Report abuse  
Picture of steve steve - 2006-10-18 04:42:19
How do I verify if no_customers is an integer using PHP?

if (is_int($_GET['no_customers'])) {

}
else {
exit("<p>You must enter values in the Affected field of the Outage
Entry form! Click your browser's Back button to
return to the previous page.</p>");
}

  2. Re: php integer verification   Reply   Report abuse  
Picture of Johan Barbier Johan Barbier - 2006-10-18 07:46:45 - In reply to message 1 from steve
Hi,

I fail to see a link with my package, but...anyway:

First, you must understand that a GET variable (or a POST one...) is ALWAYS a string.
so :
if (is_int ($_GET['someVar'])) always returns false.
Well, when the GETor POST variable comes from a form, the url or something like that, anyway.

To do what you want to do, you have several options :
- is_numeric (), but it will only check if the variable is numeric, not if it's an integer: 2.25 IS numeric, but IS NOT an integer.
- regular expressions : you can easily check if it's an integer that way, but you have to understand these expressions
- ctype extension, and its function : ctype_digit (). But it must be enabled on your server.
- and a trick using the loose "typing" in PHP : you can cast your GET variable to an integer, and then make a loose comparison (that is, not a strict one : ===) between the returned variable, and your get :
if (intval ($_GET['someVar']) == $_GET['someVar']) {
// you are here if $_GET['someVar'] is an integer.
}

Hope it helps :-)

Cheers,

Johan

  3. Re: php integer verification   Reply   Report abuse  
Picture of steve steve - 2006-10-31 04:25:06 - In reply to message 1 from steve
Thanks for the answer. I am new to php and mysql.