In PHP, is_scalar() checks whether a value is a scalar. The scalar types are int, float, string and bool.
Scalars are the simple, single values in PHP. Arrays, objects, resources and null are not scalars. is_scalar() is useful when you want to be sure that a value can be printed or compared directly, for example before you echo it.
Syntax
Call is_scalar() with the values you need. Start with this line.
PHP
$ok = is_scalar($value);
Call
What it does
is_scalar('hello')
Returns true because a string is a scalar.
is_scalar(1.5)
Returns true because a float is a scalar.
is_scalar([1, 2])
Returns false because an array is not a scalar.
null is not a scalar, even though it is a simple value. Booleans are scalars, so is_scalar(false) is true.
Library declarations
PHP
is_scalar(mixed $value): bool
Parameters
Parameter
Description
value
Required. The value you want to check. It can be of any type.
Return Value
Return Value
Description
bool
Returns true if the value is an int, float, string or bool, and false for null, arrays, objects and resources.
PHP Examples
Example 1 — The Four Scalar Types
Test an int, a float, a string and a boolean. All four are scalars.
The array mixes six values. It has an int, a string, a float, an array, null and a bool.
array_filter() calls is_scalar() on each item. Only the items that return true are kept.
Four items remain. The array [4] and null are dropped, but false is kept because it is a bool.
gettype() prints the types of the survivors. They are integer, string, double and boolean.
Frequently Asked Questions
It checks whether a value is a scalar type. It returns true for int, float, string and bool values, and false for null, arrays, objects and resources.
No. null is its own type, so is_scalar(null) returns false. Check with is_null() if you need to handle it as well.
Yes. true and false are scalars, so is_scalar(false) returns true. This is different from checking whether a value is empty.
There are four: integer, float, string and boolean. Everything else, such as arrays, objects, resources and null, is not scalar.
Use it when a value must be a simple type, for example before you echo it, store it in a text column or use it as an array key. It protects you from arrays and objects.