Types7 min readExamples run on PHP 8.5.10

is_scalar()

Learn how PHP 8.4 is_scalar() classifies int, float, string and bool values, why null, arrays, objects and resources are not scalar, and how to avoid common mistakes.

Signature

is_scalar(mixed $value): bool
Available since PHP 4.0.57.48.08.18.28.38.48.5All versions →

Purpose

is_scalar() answers a simple question: is this value a single, plain piece of data? In PHP the scalar types are int, float, string and bool. If the value has one of those types, the function returns true. For anything else it returns false.

The function is useful at boundaries where data arrives in an unknown shape: decoded JSON, configuration arrays, template variables, log context, or arguments to a generic helper. A quick scalar check lets you decide whether a value can be printed or cast to a string directly, or whether it needs special handling first.

Signature

Parameters and defaults

  • $value: any expression. Because the type is mixed, every value is accepted. There are no default values and no optional parameters.

Return value and failure cases

The function returns a bool:

  • true for integers, floats, strings and booleans.
  • false for null, arrays, objects and resources.

It does not throw for any input, and it does not emit warnings. A type error is not possible because mixed accepts everything. The function only inspects the type of the value; it never converts or validates the content.

Example 1: classifying a mix of values

The first example runs is_scalar() over several kinds of values and reports the results as JSON.

Example
<?php
$values = [42, 3.14, "hello", true, null, [1, 2], new stdClass()];
$result = [];
foreach ($values as $value) {
    $result[] = is_scalar($value);
}
echo json_encode($result), "\n";
Output
[true,true,true,true,false,false,false]

The first four entries are scalars. The last three (null, an array and an object) are not.

Example 2: a safe description helper

A common pattern is to print scalars directly and fall back to a type name for everything else. Here var_export() renders the scalar and get_debug_type() names the other types.

Example
<?php
function describe(mixed $value): string
{
    if (is_scalar($value)) {
        return var_export($value, true);
    }
    return get_debug_type($value);
}

echo describe(10), "\n";
echo describe("PHP"), "\n";
echo describe(false), "\n";
echo describe([1, 2]), "\n";
echo describe(null), "\n";
Output
10
'PHP'
false
array
null

Notice that false is still a scalar, so it is exported as the text false rather than being treated as "empty".

Example 3: filtering an array

Because is_scalar() takes one argument, you can pass its name to array_filter() as a callback. Keys are preserved.

Example
<?php
$input = ['name' => 'Ada', 'age' => 36, 'tags' => ['a', 'b'], 'active' => true, 'note' => null, 'ratio' => 0.5];
$scalars = array_filter($input, 'is_scalar');
echo json_encode($scalars, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"name":"Ada","age":36,"active":true,"ratio":0.5}

The tags array and the null note are dropped; everything else remains.

Edge cases

  • null is not scalar. Even though it is a simple value, PHP does not count it. Check for it separately with is_null() or === null when needed.
  • Falsy scalars are still scalars. 0, 0.0, "", "0" and false all return true.
  • Numeric strings are strings. "123" is a scalar because it is a string, not because it looks like a number.
  • Special floats count. NAN and INF are floats, so they are scalar.
  • Objects are never scalar. This includes objects that implement __toString(). Being convertible to a string does not make an object a scalar.
  • Resources are not scalar. The manual notes that resources are abstract data types currently based on integers, and that this implementation detail should not be relied on. Treat them as non-scalar.

Common mistakes

  1. Using it as a number check. is_scalar("abc") is true. If you need to know whether a value is numeric, use is_numeric().
  2. Expecting null to pass. Optional values that may be null need an explicit allowance, such as $value === null || is_scalar($value).
  3. Assuming it guarantees a useful string. A scalar can be cast to a string, but false becomes an empty string and a float may be formatted in a surprising way. Decide how you want each type presented.
  4. Expecting Stringable objects to pass. If you want to accept those too, check for Stringable in addition to is_scalar().
  5. Using it where a specific type is required. is_scalar() is broad. When you need exactly an integer or a string, use is_int() or is_string() instead.
  • is_int(), is_float(), is_string() and is_bool() test for one specific scalar type.
  • is_numeric() tests for numbers and numeric strings.
  • is_array() and is_object() test for the compound types that is_scalar() rejects.
  • get_debug_type() returns a readable type name for diagnostics.

Recap

is_scalar() returns true for int, float, string and bool, and false for null, arrays, objects and resources. It accepts any value, never throws, and does not look at content. Use it to separate simple values from structured ones, and reach for the narrower is_* functions when you need a specific type.

Tested examples

Classifying a mix of values
<?php
$values = [42, 3.14, "hello", true, null, [1, 2], new stdClass()];
$result = [];
foreach ($values as $value) {
    $result[] = is_scalar($value);
}
echo json_encode($result), "\n";
Output
[true,true,true,true,false,false,false]
A safe description helper
<?php
function describe(mixed $value): string
{
    if (is_scalar($value)) {
        return var_export($value, true);
    }
    return get_debug_type($value);
}

echo describe(10), "\n";
echo describe("PHP"), "\n";
echo describe(false), "\n";
echo describe([1, 2]), "\n";
echo describe(null), "\n";
Output
10
'PHP'
false
array
null
Filtering an array to scalar values
<?php
$input = ['name' => 'Ada', 'age' => 36, 'tags' => ['a', 'b'], 'active' => true, 'note' => null, 'ratio' => 0.5];
$scalars = array_filter($input, 'is_scalar');
echo json_encode($scalars, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"name":"Ada","age":36,"active":true,"ratio":0.5}