IT & Programming

Validate a float with PHP

  • Validate a float value with php.
  • How to validate a float with php.
  • Validate a float value with php.

Function accepts a parameter and returns false if value is not a float.

Function

function isValidFloat($string){

      if(!preg_match("/^[0-9]+(.[0-9]+)?$/",$string)){
         return false;
     }
}

Calling The Function

$string=5.25;

if(isValidFloat($string)!==false){
      echo 'Value is a float';
}else{
     echo 'Value is not a float';
}

Output

Value is a float

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *