is_scalar()
PHP is_scalar() checks if a variable is an integer, float, string, or boolean. Returns false for arrays, objects, NULL, and resources.
Introduction
is_scalar() is a built-in PHP function that tells you whether a variable holds a scalar value. A scalar is a single, atomic value — one that is not built out of other values. PHP has exactly four scalar types:
- integer — whole numbers, e.g.
42 - float (double) — numbers with a decimal point, e.g.
3.14 - string — text, e.g.
"hello" - boolean —
trueorfalse
Everything else is non-scalar: arrays and objects are compound (they group many values), while NULL and resource values are special types that is_scalar() deliberately reports as false. Knowing this distinction matters because scalar values are safe to print, concatenate, or store directly, whereas compound values usually need to be looped over or inspected first.
Syntax
is_scalar(mixed $value): boolThe function takes a single argument, $value, and returns true when it is an integer, float, string, or boolean, and false for every other type — including arrays, objects, NULL, and resources.
Example Usage
The following example checks one variable of each common type:
The string, float, and boolean are all scalars, so they return true. The array is a compound type, so it returns false.
Tip:
var_dump()is clearer thanechofor boolean results. Withecho,trueprints as1andfalseprints as an empty string, which is easy to misread. See var_dump() for more.
NULL, objects, and resources are not scalar
These three cases trip people up most often, because intuitively they feel "simple," but is_scalar() returns false for all of them:
<?php
var_dump(is_scalar(null)); // bool(false) — NULL is its own type
var_dump(is_scalar(new stdClass())); // bool(false) — objects are compound
$handle = fopen("php://memory", "r");
var_dump(is_scalar($handle)); // bool(false) — a file resource
fclose($handle);
?>If you also want to treat NULL as acceptable, check for it separately: is_scalar($v) || is_null($v).
Using is_scalar() as a guard
A common use is to decide whether a value can be safely printed or concatenated. Compound values like arrays cannot be turned into a string directly (echo $array raises a warning), so guarding first prevents errors:
<?php
function describe(mixed $value): string
{
if (is_scalar($value)) {
return "scalar value: $value";
}
return "non-scalar value of type " . gettype($value);
}
echo describe(42), "\n"; // scalar value: 42
echo describe([1, 2]), "\n"; // non-scalar value of type array
echo describe(null), "\n"; // non-scalar value of type NULL
?>Here gettype() reports the exact type of the non-scalar values, while is_scalar() keeps the printable ones out of trouble.
is_scalar() vs. the type-specific checks
is_scalar() is a convenience for "is this any of the four scalar types?" When you care about one specific type, use the dedicated function instead:
| Function | Returns true for |
|---|---|
is_int() | integers only |
is_float() | floats only |
is_string() | strings only |
is_bool() | booleans only |
is_scalar() | integer or float or string or boolean |
is_scalar() is equivalent to writing is_int($v) || is_float($v) || is_string($v) || is_bool($v), but it is shorter and clearer.
Conclusion
is_scalar() returns true only for the four scalar types — integer, float, string, and boolean — and false for arrays, objects, NULL, and resources. It is most useful as a guard before printing or concatenating a value whose type you are not sure of. When you need to verify one specific type, reach for is_int(), is_string(), and friends; when you just need "is this a simple single value?", is_scalar() is the right tool.