PHP is_int() Function

What is PHP is_int() Function?

If you want to check whether a value is of integer type or not, use is_int() function. Instead of using value directly, you can use a variable to check its type too.

Few important things about integer-

  • An integer is a whole number, no decimal part included in it. Ex. 1, 100000 etc.
  • PHP supports signed integer i.e. it can be both positive or negative.
  • You can define an integer in four formats –
    • Decimal (10 based): Example- 100
    • Hexadecimal (16 based, starts with 0x or 0X): Example- 0xA
    • Octal (8 based, starts with 0o or 0O or 0): Example- 0o12
    • Binary (2 based, starts with 0b or 0B): Example 0b1010
  • Integer size is platform-dependent. Using the following constants, you can find out about your system’s integer support-
    • PHP_INT_MAX: Largest possible integer value. In my system, it is 9223372036854775807.
    • PHP_INT_MIN: Smallest possible integer value. In my system it is -9223372036854775808.
    • PHP_INT_SIZE: Integer size in bytes. In my system, it is 8.

All the following formats are valid integer as of PHP 8.1.0-

FormatExample (Integer)
decimal: [1-9][0-9]*(_[0-9]+)*
                | 0
1, 10, 100, 10_1, 10_190_17, 0 etc.
hexadecimal: 0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*0x0, 0X0, 0x0_01 etc.
octal: 0[oO]?[0-7]+(_[0-7]+)*0o1, 01, 0o7_7 etc.
binary: 0[bB][01]+(_[01]+)*0b0, 0b1_1, 0b101010  etc.

Check example 3.

Syntax:

is_int(value)

Parameters:

The Function has 1 parameter which is required-

value (Required): It is the value or variable whose type to be evaluated. This parameter can accept any type of value/variable. Check example 1.

Return Values:

The function returns-

  • TRUE – if the value is of integer type.
  • FALSE – if the value is not of type integer.

Check example 2.

How to use is_int() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result the conditional statement returns, your program flow progresses. Have a look at the code-

<?php
$Age = $_POST[‘Age'];
if(is_int($Age)){
    echo "Age is: " . $Age;
    // Other codes here.
}else{
    echo "Age is not an integer.";
    // Other codes here.
}
?>

Examples:

Example 1: is_int() can check both type of a value or a variable-

<?php
echo "Is 101 integer? "; echo  is_int(202)? "Yes":"No"; echo "<br />";
$level = 202;
echo 'Is int_int($level) integer? '; echo  is_int($level)? "Yes":"No"; echo "<br />";
?>

Output:

Is 101 integer? Yes
Is int_int($level) integer? Yes

Explanation:

Line 2: The function can check type of the value (integer here) directly.

Line 4: The function can check type of a value of an integer that is stored in a variable.

Example 2: is_int() function examples-

<?php
echo 'Is -202 integer? '; echo  is_int(-202)? "Yes":"No"; echo "<br />";
echo 'Is "202" integer? '; echo  is_int("-202")? "Yes":"No"; echo "<br />";
echo 'Is 202.0 integer? '; echo  is_int(202.0)? "Yes":"No"; echo "<br />";
echo 'Is 202.0 integer? '; echo  is_int(202E1)? "Yes":"No";
?>

Output:

Is -202 integer? Yes
Is “202” integer? No
Is 202.0 integer? No
Is 202.0 integer? No

Example 3: More is_int() function examples-

<?php
echo 'Is 0 integer? '; echo  is_int(0)? "Yes":"No"; echo "<br />";
echo 'Is 10_190_17 integer? '; echo  is_int(10_190_17) ? "Yes":"No"; echo "<br />";
echo "Is binary 0b1111010100 (decimal ". 0b1111010100.")  integer? "; echo  is_int(0b1111010100)? "Yes":"No"; echo "<br />";
echo "Is octal 01724 (decimal ". 01724 .")  integer? "; echo  is_int(01724)? "Yes":"No"; echo "<br />";
echo "Is hexadecimal 0x3D4 (decimal ". 0x3D4 .")  integer? "; echo  is_int(0x3D4)? "Yes":"No";
?>

Output:

Is 0 integer? Yes
Is 10_190_17 integer? Yes
Is binary 0b1111010100 (decimal 980) integer? Yes
Is octal 01724 (decimal 980) integer? Yes
Is hexadecimal 0x3D4 (decimal 980) integer? Yes

Explanation:

Decimal, octal, hexadecimal, and binary are valid as an integer.

Why type checking is important?

Type checking is crucial part of any project. It can break your code if not done correctly.

  • Users may provide different type of data than they are asked to provide. For example, in the phone field, they may add string value. Though, you may apply client-side validation to prevent this error, you must implement server-side validation too with PHP.
  • As PHP can automatically change data type (Type Juggling) of a variable, you need to be careful situations like this when type conversion may occur. In the following code snippet, adding 10.1 with integer 100 at line 3 converts the data type the variable from integer to float.
    <?php
    $marks = 100;
    $marks = $marks + 10.1;
    var_dump($marks);
    ?>
    

Practical Usages of is_int() Function:

  • Suppose you declared a table column named “Quantity” which only accepts integer. Before inserting data in this column, you can check the data type with the is_int() function. Check the code snippet below-
    <?php
    $Quantity = $_POST['Quantity'];
    if(is_int($Quantity)){
        mysqli_query($connection, "INSERT INTO `product` (`Quantity`, ...) 
                            VALUES ('$ Quantity ',...)");
    }else{
        echo "Error. Quantity must be an integer.";
    }
    ?>
    
  • Suppose you have a CSV file that contains two type of values – integer and character. All are enclosed by quotes. To separate the numbers from the characters, use is_int() function. Look at the example below. It outputs – 838 274 567 2653.
    <?php
    $rows = [
        'Alabama', 838, 'Alaska', 274, 'Arizona', 567, 'California', 2653
    ];
    foreach ($rows as $row) {
        if (is_int($row)) {
            echo $row." ";
        } 
    }
    

Notes on is_int() Function:

  • is_int() function doesn’t return true even though it is a valid integer if the valid integer contains thousand separators. The function below returns “ArgumentCountErro Fatal Error”.
    <?php
    var_dump(is_int(1,000,000));
    ?>
    
  • The followings are not considered as valid numeric string – NULL, Boolean value, array. In all cases, the function returns FALSE. Check example below-
    <?php // #8
    var_dump(is_int(NULL)); echo "<br />";
    var_dump(is_int(FALSE)); echo "<br />";
    if(!is_int([202])){
        echo '[202] is not a integer. It is an '; echo gettype([202]);
    }
    ?>
    

    Output:

    bool(false) 
    bool(false) 
    [202] is not a integer. It is an array

Caution:

  • Remember, form input is always string. That means whether you add an integer in a form field, it becomes string when you retrieve it.
    <?php
    $Age = $_POST['Age'];
    if(is_int($Age)){
        echo "Age is: " . $Age;
    }else{
        echo "Invalid Age.";
    }
    ?>
    
  • If you check a valid integer that is larger than PHP_INT_MAX or smaller than PHP_INT_MIN, the function will return FALSE as PHP interprets it a different type. Check the code below-
    <?php
    $var = PHP_INT_MAX + 1;
    if(!is_int($var)){
        echo "$var is not a integer. It is a "; echo gettype($var);
    }
    ?>
    

    Output:

    9.2233720368548E+18 is not a integer. It is a double

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Similar functions to is_int():

  • is_float()
  • is_integer() –
  • is_numeric()
  • is_string()
  • is_object() –
  • is_callable() –
  • is_null() –
  • is_resource() –
  • is_scalar() –
  • is_bool() –
  • is_iterable() –
  • is_long() –
  • is_real() –
  • is_double() –
  • is_array() –

Summary: PHP is_int() Function

The correct data type can make or break your code. The function gives you the ability to check whether a variable is an integer type variable. is_int() is one of the useful built-in variable-handling functions.

Reference:

https://www.php.net/manual/en/function.is-int.php

https://www.php.net/manual/en/language.types.integer.php