Signature
is_scalar(mixed $value): boolPurpose
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 ismixed, every value is accepted. There are no default values and no optional parameters.
Return value and failure cases
The function returns a bool:
truefor integers, floats, strings and booleans.falsefornull, 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.
$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";[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.
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";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.
$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";{"name":"Ada","age":36,"active":true,"ratio":0.5}
The tags array and the null note are dropped; everything else remains.
Edge cases
nullis not scalar. Even though it is a simple value, PHP does not count it. Check for it separately withis_null()or=== nullwhen needed.- Falsy scalars are still scalars.
0,0.0,"","0"andfalseall returntrue. - Numeric strings are strings.
"123"is a scalar because it is a string, not because it looks like a number. - Special floats count.
NANandINFare 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
- Using it as a number check.
is_scalar("abc")istrue. If you need to know whether a value is numeric, useis_numeric(). - Expecting
nullto pass. Optional values that may benullneed an explicit allowance, such as$value === null || is_scalar($value). - Assuming it guarantees a useful string. A scalar can be cast to a string, but
falsebecomes an empty string and a float may be formatted in a surprising way. Decide how you want each type presented. - Expecting Stringable objects to pass. If you want to accept those too, check for
Stringablein addition tois_scalar(). - Using it where a specific type is required.
is_scalar()is broad. When you need exactly an integer or a string, useis_int()oris_string()instead.
Related functions
is_int(),is_float(),is_string()andis_bool()test for one specific scalar type.is_numeric()tests for numbers and numeric strings.is_array()andis_object()test for the compound types thatis_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
$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";
[true,true,true,true,false,false,false]
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";
10 'PHP' false array null
$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";
{"name":"Ada","age":36,"active":true,"ratio":0.5}