Wednesday, December 3, 2008

How to check if a given value is numeric (integer) using php

Following code will show you how to validate a variable to see if its a numeric integetr or a string. you can use is_numeric() and is_int() functions to validate this. Use
is_numeric() when validating form data, which will always be strings.

is_int() will check if the variable type is integer. so form data will always be false.

is_numeric() will check if the variable contain all numbers, use this function to validate form data.


<?php

$num1 = 48;
$num2 = '48';
$num3 = 'hello';

if(is_int($num1))
{
echo 'This is integer';
}
else
{
echo 'Not integer';
}

//check for above other $num2 and $num3 variables.

/*
USE is_numeric() IF YOU ARE VALIDATING FORM DATA
INSTEAD OF is_int()

*/

?>


Get custom programming done at GetAFreelancer.com!

No comments:

Post a Comment