php

PHP is_numeric() Function

PHP has many built-in functions to test the type of data stored in the variable. The is_numeric() function is one of them. It is used to test a variable is a number or string of numbers. The string of numbers can contain the ‘+/-‘ symbol, decimal point, and exponential part with the digits. The uses of this function for different purposes in PHP have shown in this tutorial.

Syntax:

The syntax of is_numeric() function has given below.

bool is_numeric (mixed $variable);

This function has one argument that can be a number or a string. It returns true if the value of the argument is a number or a string of numbers, otherwise returns false.

Different Uses of is_numeric() Function

The purposes of using the is_numeric() function in the script have shown in the part of this tutorial by using multiple examples.

Example-1: Check different values using is_numeric() function

Create a PHP file with the following script to check the returned value of the is_numeric() function for number, string, and the number of string values. The check_number() function is defined in the script to take each value in the argument and check the value is a number or not by using the is_numeric() function.

<?php

//Define function to check a value is number or not
function check_number($val)
{
    if (is_numeric($val))
        echo "<h3>$val is a number.</h3>";
    else
        echo "<h3>$val is not a number.</h3>";
}

echo "<center>";
//Assign a number value
$value = 100;
check_number($value);

//Assign a string value
$value = "two";
check_number($value);

//Assign a string of number
$value = "200";
check_number($value);
echo "</center>";
 
?>

Output:

The following output will appear after executing the above script.

Example-2: Check the submitted form data is a number or not

Create a PHP file with the following script to check the submitted form data is a number or not by using the is_numeric() function. The data submitted by a form is a string and the is_numeric() function returns true for the number and the number of strings. The script will print a message if any non-digit characters will be submitted by the form, otherwise, the submitted value will be printed.

<tml>
<body>
    <center><div>
        <h3>Unset session Example </h3>
        <form method="post" action="#">
             Enter a number: <input type="text" name="num" />
            <br/><br/>
            <input type="submit" name="submit" value="Submit" />
        </form>
    </div></center>
</body>
</html>

<?php

//Check the form is submitted or not
if(isset($_POST['submit']))
{
    //Check the number is submitted or not
    if(isset($_POST['num']))
    {
        //Read the submitted value
        $n = $_POST['num'];
        //Check the value is number or not
        if (!is_numeric($n))
            echo "<center><p style='color:red'>You have to enter a number</p></center>";
        else
            echo "<center><p style='color:green'>The submitted number is $n</p></center>";
     }
}

Output:

The following form will appear after executing the above script.

The following output will appear if the numeric value, 89 will be submitted by the form.

The following output will appear if the string value, ‘hello’ will be submitted by the form.

Example-3: Check the values of a numeric array

Create a PHP file with the following script to check each value of a number array is a number or not by using the is_numeric() function. An array of 7 elements has been declared in the script. The array contains the string, integer, float, binary, and hex values. The foreach loop has used to iterate the value of the array and check the value is a number or not.

<?php

//Define a numeric array of mix types of data
$arr = ["LinuxHint", 56, "One", 0b111,"78", 0xBAD, 7.98];

//Iterate the values of the array to check the numeric and non-numeric values
foreach ($arr as $val) {

    if (is_numeric($val))
        echo "$val is a number.<br/>";
    else
        echo "$val is not a number.<br/>";
}
?>

Output:

The following form will appear after executing the above script. In the output, the binary and hex numbers have printed in the decimal format with the other two decimal numbers and the other two values of the array are string.

Example-4: Filter the values of an associative array

Create a PHP file with the following script to check each value of an associative array is a number or not by using the is_numeric() function to print those values which are numbers and count the non-numeric values. An associative array of 6 elements has been declared in the script. The key of the array contains the name of the student and the value of the array contains the obtained marks. According to the array values, two students are absent. The foreach loop has used to iterate the array values and find out those values which are numeric and print those values with the corresponding key in the output. The $counter variable has used to count the non-numeric values of the array and printed later.

<?php

//Define an associative array of mix types of data
$resultArr = array("Md. Abir" => 89, "Abbas Uddin" => "Absent", "Sadia Akter" => 84,
                   "Jafar Iqbal" => 68, "Kamal Hossain" => 75, "Farzana Rahman" => "Absent");

//Initialize a counter to count the number of absent students
$counter = 0;
//Iterate the values of the array to check the numeric and non-numeric values
foreach ($resultArr as $key=>$val) {

    if (is_numeric($val))
        echo "<p>$key has ontained $val.</p>";
    else
        $counter++;
}
//Check the counter value
if($counter)
    echo "<h3>Absent: $counter students</h3>";
else
    echo "<h3>All students are present in the exam.</h3>"
?>

Output:

The following form will appear after executing the above script. In the output, four values with the keys have been printed. Two values in the array were ‘absent’. So, the total number of absent is 2 that has printed in the end of the output.

Conclusion

The is_numeric() is a useful function of PHP to validate the numeric data. The way of checking a variable, form value, and array value is a number or not by using this function has been described in this tutorial. I hope, the PHP users will be able to use this function properly in their script after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.