Current Location: Home> Latest Articles> How do PHP is_int function be used in a loop?

How do PHP is_int function be used in a loop?

gitbox 2025-05-26

In PHP programming, it is very common to determine whether a value is an integer, especially when dealing with loop structures (such as foreach or for loops). is_int() is a built-in function that determines whether a variable is a value of type integer ( int ). Although the function itself is simple, it is often accompanied by some easily overlooked pitfalls when used in loops, especially when dealing with array keys, form inputs, or external data.

This article will explain in depth how to correctly use is_int() in loops to judge integers, including common misunderstandings and practical examples.

1. is_int() basic syntax

 is_int(mixed $value): bool

This function takes a variable as a parameter, and returns true if the type of the variable is an integer (int), otherwise returns false .

Example:

 $value = 10;
if (is_int($value)) {
    echo "It's an integer";
} else {
    echo "不It's an integer";
}

Output:

 It's an integer

2. Use is_int() in the foreach loop

In practical applications, we often use foreach to traverse arrays. At this time, integer judgments may be made on the keys of the array. Typical scenarios are to process mixed arrays or distinguish index arrays from associative arrays.

 $data = [
    0 => 'apple',
    1 => 'banana',
    'a' => 'orange',
    '2' => 'pear'
];

foreach ($data as $key => $value) {
    if (is_int($key)) {
        echo "$key It's an integer索引:$value\n";
    } else {
        echo "$key Yes, non-integer index:$value\n";
    }
}

Output:

 0 It's an integer索引:apple
1 It's an integer索引:banana
a Yes, non-integer index:orange
2 Yes, non-integer index:pear

Note: Although the key '2' looks like an integer, it is of a string type and will not be judged as an integer by is_int() . This is the key point when using is_int() .

3. Solve the problem of judging string numbers

If you want to judge that string-formed numbers (such as '2' ) are also counted as integers, you should use ctype_digit() or is_numeric() with intval() :

 $key = '2';

if (is_numeric($key) && (string)(int)$key === (string)$key) {
    echo "$key Can be considered an integer";
}

But if you just want to determine whether the variable is a strict integer value, then is_int() is the most suitable and safe choice.

4. Use is_int() in the for loop

Although the for loop itself often relies on integer indexes, sometimes it is necessary to make judgments on the data passed outside to determine the behavior of the loop:

 $limit = $_GET['limit'] ?? 10;

if (!is_int($limit)) {
    $limit = (int)$limit; // Cases
}

for ($i = 0; $i < $limit; $i++) {
    echo "1. $i Second loop\n";
}

If you pass $limit through form or URL parameters, the default is the string type. Directly using is_int() will fail to judge, so you need to perform type conversion before judgment, or use it with filter_var() :

 $limit = $_GET['limit'] ?? 10;

if (filter_var($limit, FILTER_VALIDATE_INT) !== false) {
    $limit = (int)$limit;
} else {
    $limit = 10;
}

This method is safer and more suitable for verification of user input.

For example, visit:

 https://gitbox.net/index.php?limit=5

The loop will be executed 5 times.

5. Summary

  • is_int() returns true only for values ​​of type int , and string numbers such as '5' return false.

  • When judging whether an array key is an integer in foreach , be careful: numeric strings will not be processed as integer keys.

  • User input is usually a string type, and it is recommended to use filter_var() or manual conversion to ensure the type is consistent.

  • If you want to judge a string that looks like an integer, you can also use ctype_digit() or other alternatives.

Using is_int() in a loop structure is a common type safety check method, but you must understand its limitations and usage scenarios in order to write a robust PHP program.