Reputation: 2564
function number_check($int){
if($int>0 && !is_float($int)){return TRUE;}
else{return FALSE;}
}
$var="025";
if(number_check($var)){echo "pass";}else{echo "fail";}
I have make a function to check the number for post id.
post_id
should always > 0
and no decimal.
but I have a problem when user try to enter 000243
, if user put zero
at front, it return true
.
Is any way to solve this?
Upvotes: 0
Views: 1298
Reputation: 1781
I think checking $int{0} != 0
will solve what you are trying to achieve :
function number_check($int){
if ( $int > 0 && !is_float($int) && $int{0} != 0 ) {
return TRUE;
}
else {
return FALSE;
}
}
$var="023";
if ( number_check($var) ) {
echo "pass";
} else {
echo "fail";
}
Check this DEMO
Upvotes: 1
Reputation: 13511
Another way to do that is the following:
function prepareID($id)
{
$id = preg_replace('/^([0]*)/', '', $id);
return (int)$id;
}
$var = prepareID("025");
The prepareID function will remove any leading zeros and it will return an integer
Upvotes: 1
Reputation: 378
try to assign value to $var without using quotes. i.e $var = 000243;
Upvotes: 1