Types7 min readExamples run on PHP 8.5.10

is_long()

Learn how is_long(), the alias of is_int(), tests for the integer type in PHP 8.4, with examples, edge cases and common mistakes.

Signature

is_long(mixed $value): bool

Purpose

is_long() answers one question: is the value you passed an integer? It returns true only when the value's type is int. According to the PHP manual, is_long() is an alias of is_int(). Both names call the same function and behave identically. The name long comes from the C type that integers historically mapped to, but PHP has no separate long type. New code usually uses is_int(), because that name matches the int type declaration. You will still see is_long() in older code bases.

Signature

Parameters and defaults

  • $value: any value at all. There is no default, so the argument is required. Scalars, arrays, objects, null and resources are all accepted.

Return value and failure cases

The function returns true when $value has type int and false for every other type. It does not throw for unusual input and does not emit warnings. A value of the wrong type is not an error, it is just a false result.

The check looks at the type, not at the content. The string "15" looks like a number, but it is a string, so the result is false. The float 3.0 has no fractional part, but it is still a float, so the result is false.

Example 1: which types count as integers

The loop below runs several kinds of values through is_long() and prints a readable label for each result.

Example
<?php
$samples = [
    'int' => 42,
    'negative' => -7,
    'zero' => 0,
    'float' => 3.0,
    'numeric string' => '15',
    'bool' => true,
    'null' => null,
];
foreach ($samples as $label => $value) {
    echo $label, ': ', is_long($value) ? 'true' : 'false', "\n";
}
Output
int: true
negative: true
zero: true
float: false
numeric string: false
bool: false
null: false

Only the first three entries are integers. Booleans and null are never treated as integers by this function, even though PHP can convert them to integers in arithmetic.

Example 2: filtering and the alias relationship

Because the function returns a boolean and takes one argument, you can pass its name as a callback. array_filter() keeps the elements for which the callback returns true. The second line shows that the alias gives the same answers as is_int().

Example
<?php
$data = [1, '2', 3.5, 4, [5]];
$ints = array_values(array_filter($data, 'is_long'));
echo json_encode($ints), "\n";
echo json_encode(array_map('is_long', $data) === array_map('is_int', $data)), "\n";
Output
[1,4]
true

The filtered list holds only the real integers. The strict comparison of the two result arrays is true because the two names are the same function.

Example 3: results of arithmetic

The type of a computed value is not always what the numbers suggest. This example checks several expressions.

Example
<?php
$results = [
    'intdiv' => intdiv(10, 2),
    'slash' => 10 / 2,
    'slash fraction' => 10 / 4,
    'round' => round(2.4),
    'cast' => (int) round(2.4),
    'overflow' => PHP_INT_MAX + 1,
];
foreach ($results as $label => $value) {
    echo $label, ': ', is_long($value) ? 'true' : 'false', "\n";
}
Output
intdiv: true
slash: true
slash fraction: false
round: false
cast: true
overflow: false

intdiv() always yields an integer. The / operator yields an integer when both operands are integers and the division is exact, otherwise a float. round() always returns a float, so you must cast it if you need an integer. Adding one to PHP_INT_MAX leaves the integer range, so PHP produces a float.

Edge cases

  • Numeric strings: "42", "0x1A" and "1e3" are all strings, so is_long() returns false. If you need to validate user input such as form data, use filter_var() with FILTER_VALIDATE_INT or check with ctype_digit() first.
  • Floats with whole values: 5.0 is a float. Convert it with (int) only if you are sure that truncation is acceptable.
  • Booleans and null: true, false and null return false.
  • Integer limits: values beyond the platform's integer range become floats, and is_long() reports them as non-integers.
  • Objects and arrays: these return false and are never inspected.

Common mistakes

  1. Expecting is_long("123") to be true. Input from forms, query strings and most text sources arrives as strings, so you must validate or convert it before relying on a type check.
  2. Assuming that is_long() means a bigger integer than is_int(). They are the same function.
  3. Using the result of round(), floor() or ceil() directly. These return floats, so is_long() returns false for them.
  4. Using is_long() to test for any number. Use is_numeric() when strings with numeric content are allowed, or is_int() together with is_float() when you need real numbers only.
  5. Mixing the two names in the same project without a reason. Pick one, preferably is_int(), to keep the code consistent.
  • is_int(): the primary name of this function.
  • is_integer(): another alias of the same check.
  • is_float(): tests for the float type.
  • is_numeric(): accepts numbers and numeric strings.
  • filter_var(): validates and converts text input.
  • gettype() and get_debug_type(): report the type name of a value.

Recap

is_long() is an alias of is_int() with the signature is_long(mixed $value): bool. It returns true only for values whose type is int. Numeric strings, whole-number floats, booleans and null all return false. Convert or validate input first when you expect text, and prefer the name is_int() in new code.

Tested examples

Which types count as integers
<?php
$samples = [
    'int' => 42,
    'negative' => -7,
    'zero' => 0,
    'float' => 3.0,
    'numeric string' => '15',
    'bool' => true,
    'null' => null,
];
foreach ($samples as $label => $value) {
    echo $label, ': ', is_long($value) ? 'true' : 'false', "\n";
}
Output
int: true
negative: true
zero: true
float: false
numeric string: false
bool: false
null: false
Filtering and the alias relationship
<?php
$data = [1, '2', 3.5, 4, [5]];
$ints = array_values(array_filter($data, 'is_long'));
echo json_encode($ints), "\n";
echo json_encode(array_map('is_long', $data) === array_map('is_int', $data)), "\n";
Output
[1,4]
true
Results of arithmetic
<?php
$results = [
    'intdiv' => intdiv(10, 2),
    'slash' => 10 / 2,
    'slash fraction' => 10 / 4,
    'round' => round(2.4),
    'cast' => (int) round(2.4),
    'overflow' => PHP_INT_MAX + 1,
];
foreach ($results as $label => $value) {
    echo $label, ': ', is_long($value) ? 'true' : 'false', "\n";
}
Output
intdiv: true
slash: true
slash fraction: false
round: false
cast: true
overflow: false