is_scalar

Checks whether a value is a simple int, float, string or bool rather than a structure.

Signature
is_scalar(mixed $value): bool
  • Added in PHP 4.0.5

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if value is an int, float, string or bool; false otherwise.

is_scalar() returns true for the four simple types: int, float, string and bool. Everything else — arrays, objects, null and resources — returns false.

When to reach for it

It shines when you are walking arbitrary nested data and need to know whether a node is a leaf or something to recurse into. A flattener, a config merger, a query-string builder or a debug dumper all reduce to "if it is scalar, format it; otherwise descend". It is also a good guard before string interpolation or (string) casting, since scalars all convert cleanly while arrays produce "Array to string conversion" warnings.

What trips people up

null is not scalar. This surprises almost everyone, because null is obviously not a structure. If your leaf test should accept nulls — and for JSON data it almost always should — write is_scalar($v) || $v === null.

Resources are not scalar either, so a file handle from fopen() fails the test even though it is a single value.

Finally, an object with __toString() is not scalar. If you want "anything I can turn into a string", test is_scalar($v) || $v instanceof Stringable instead.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Recursing through nested arrays and stopping at leaf values
  • Guarding a (string) cast so arrays never trigger a conversion warning
  • Rejecting arrays and objects from a query-string or CSV builder

Frequently asked questions

Is null a scalar in PHP?

No. is_scalar(null) returns false, because null is its own type rather than one of int, float, string or bool. Add an explicit $v === null check if you want to accept it.

What counts as a scalar in PHP?

Exactly four types: int, float, string and bool. Arrays, objects, resources and null all return false from is_scalar().

What is the difference between is_scalar and is_numeric?

is_scalar() accepts any simple value including strings and booleans, while is_numeric() only accepts numbers and numeric strings. "hello" is scalar but not numeric.

More examples

Flattening nested config to dotted keys

The scalar test decides where recursion stops. The extra === null branch is essential — without it the null tls value would be silently dropped, because is_scalar(null) is false.

Example · php
<?php
function flatten(array $data, string $prefix = ''): array {
    $out = [];
    foreach ($data as $k => $v) {
        $key = $prefix === '' ? (string) $k : "$prefix.$k";
        if (is_scalar($v) || $v === null) {
            $out[$key] = $v;
        } elseif (is_array($v)) {
            $out += flatten($v, $key);
        }
    }
    return $out;
}

print_r(flatten(['db' => ['host' => 'localhost', 'port' => 5432, 'tls' => null]]));

Safe string conversion

Booleans are scalar but cast to "1" and "", which is rarely what you want in output, so they are handled first. The is_scalar() branch then covers int, float and string with one check, and the array falls through to a type name instead of a warning.

Example · php
<?php
function asText(mixed $v): string {
    if ($v === null)          { return 'NULL'; }
    if (is_bool($v))          { return $v ? 'true' : 'false'; }
    if (is_scalar($v))        { return (string) $v; }
    if ($v instanceof Stringable) { return (string) $v; }
    return get_debug_type($v);
}

foreach ([1, 2.5, 'x', false, null, [1, 2]] as $v) {
    echo asText($v), "\n";
}

Discussion

  • Be the first to comment on this lesson.