(12 ratings)   
By: goodphptutorials.com
If you become clear with the concept of integer and string in PHP then you’ll obviously come to know that why those functions are not working sometime to validate integers in PHP. Let’s look at this by an example
Added: 06 May 2008    Views: 216  
PathComputers    Programming    Php
Keywords: computer   computers   coder   program   programming   script   code   coder   function   object   php  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 

If you become clear with the concept of integer and string in PHP then you’ll obviously come to know that why those functions are not working sometime to validate integers in PHP. Let’s look at this by an example

$a='10';
$b
=10;

As you can see above, $a is string and $b is a integer and thats what does matter in PHP. Now, let’s try is_int() to validate the integers in PHP.

echo is_int($a); //returns false
echo is_int($b); //return true

And many of the PHP programmers tries to validate the posted values such as is_int($_POST['quantity']) and it just returns the false values although there are only digits in posted quantity.Why it is so? Because, $_POST['quantity'] is just a string and nothing more than a string. Values posted from the form’s element can’t be other variable type than string.

How to validate integers (digits) in PHP?

We can use regular expression to check weather posted value contains the integers only or not. But the regular expression might be a tedious task for people who are not good in using regular expression.

The best solution is using a function called ctype_digit() available in PHP. It just check weather the strings contains only digits or not. If you try using ctype_digit($_POST['quantity']) then It will just give you the desired result.

About the Author :
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments