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

by Lalita 04-Aug-19

0 142


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


Share:


Comments

    Waiting for your comments

Leave a Reply