How to check whether a variable is numeric or not in php
×


How to check whether a variable is numeric or not in php

3118

To check whether a variable or value is numeric or not, there is a simple method used in PHP and i.e. is_numeric function. This functions checks whether the condition is true or false.

Example:

<?php
  $check_number= '989';
  if(is_numeric($check_number)){
	echo "This is a numeric value.";
  }
  else{
	echo "This is not a numeric value.";
  }
?>

Output: This is a numeric value.

<!-- That means, variable value is true -->


There is lot of examples. Like:

<?php
  $check_number= '122786.50';
  if(is_numeric($check_number)){
    echo "This is also a numeric value."; 
  }
  else{
    echo "This is not a numeric value.";
  }
?>

Output: This is also a numeric value.

If I change the value with alphabets, then it will return false.

Example:

<?php
  $check_number= 'AD122786.50';
  if(is_numeric($check_number)){
     echo "This is a numeric value.";
  }
  else{
     echo "This is not a numeric value.";
  }
?>

Output: This is not a numeric value



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments