PHP is_scalar() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

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);
CallWhat 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

ParameterDescription
valueRequired. The value you want to check. It can be of any type.

Return Value

Return ValueDescription
boolReturns 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.

PHP
<?php
var_dump(is_scalar(10));
var_dump(is_scalar(1.5));
var_dump(is_scalar('hi'));
var_dump(is_scalar(true));

How It Works

  1. 10 is an int. Integers are scalar values.
  2. 1. 5 is a float. Floats are also scalar values.
  3. 'hi' is a string. A string counts as one scalar value, not a list of letters.
  4. true is a bool. Booleans are the fourth scalar type, so every result is true.

Example 2 — Values That Are Not Scalars

Check null, an array, an object and a resource. None of them is a scalar.

PHP
<?php
var_dump(is_scalar(null));
var_dump(is_scalar([1, 2]));
var_dump(is_scalar(new stdClass()));
var_dump(is_scalar(fopen('php://memory', 'r')));

How It Works

  1. null is not a scalar. It means no value, so the result is false.
  2. An array holds many values. It is a compound type, so it is not scalar.
  3. An object is not a scalar either. Objects are their own type in PHP.
  4. A file handle is a resource. Resources are not scalars, so the last result is false.

Example 3 — Filtering Scalars from a List

Use is_scalar() with array_filter() to keep only the simple values from a mixed array.

PHP
<?php
$items = [1, 'two', 3.5, [4], null, false];

$scalars = array_filter($items, 'is_scalar');
echo count($scalars), PHP_EOL;

foreach ($scalars as $s) {
    echo gettype($s), PHP_EOL;
}

How It Works

  1. The array mixes six values. It has an int, a string, a float, an array, null and a bool.
  2. array_filter() calls is_scalar() on each item. Only the items that return true are kept.
  3. Four items remain. The array [4] and null are dropped, but false is kept because it is a bool.
  4. 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.

Next: is_string()

is_string() checks whether a value is a string. It returns true for any text value, includ.

is_string() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful