PHP is_string() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, is_string() checks whether a value is a string. It returns true for any text value, including an empty string, and false for numbers, booleans, arrays, objects and null.

The check is about the type, not the content. The string '123' is a string even though it contains digits, and the integer 123 is not a string. is_string() is a quick way to validate input before you call string functions such as strlen() or strtoupper().

Syntax

Call is_string() with the values you need. Start with this line.

PHP
$ok = is_string($value);
CallWhat it does
is_string('hello')Returns true because the value is text.
is_string('')Returns true because an empty string is still a string.
is_string(123)Returns false because 123 is an integer.

Numeric strings like '123' are strings, so is_string('123') is true. Use is_numeric() when you care about the content of the string.

Library declarations
PHP
is_string(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 of type string, and false for every other type.

PHP Examples

Example 1 — Strings Versus Numbers

Check a word, an empty string, a numeric string and a real integer.

PHP
<?php
var_dump(is_string('hello'));
var_dump(is_string(''));
var_dump(is_string('123'));
var_dump(is_string(123));

How It Works

  1. 'hello' is text. Anything inside quotes is a string, so the result is true.
  2. '' is an empty string. It has no characters but is still a string.
  3. '123' is a string of digits. The quotes make it text, so it returns true.
  4. 123 has no quotes. It is an integer, so is_string() returns false.

Example 2 — Non-Strings and Conversions

See how null and arrays behave, and how converting a value with strval() or a cast makes it a string.

PHP
<?php
var_dump(is_string(null));
var_dump(is_string(['a']));
var_dump(is_string(strval(5)));
var_dump(is_string((string) 1.5));

How It Works

  1. null is not a string. It is not even an empty string, so the result is false.
  2. An array is not a string. is_string() never looks inside it.
  3. strval(5) returns the text '5'. The converted value is a string, so the result is true.
  4. (string) 1. 5 also creates text. After the cast, the float becomes the string '1.5'.

Example 3 — Guarding a String Function

Check the type before calling strtoupper(), and return a friendly message for other types.

PHP
<?php
function shout($text) {
    if (!is_string($text)) {
        return 'Please pass a string';
    }

    return strtoupper($text) . '!';
}

echo shout('hello'), PHP_EOL;
echo shout(42), PHP_EOL;

How It Works

  1. The function has no type declaration. Callers can send any value, so we check it ourselves.
  2. The guard uses !is_string(). If the value is not text, the function returns early.
  3. 'hello' passes the check. strtoupper() makes it HELLO and the dot adds an exclamation mark.
  4. 42 is an integer. The guard returns the message and strtoupper() is never called.

Frequently Asked Questions

It checks whether a value is of type string. It returns true for any text, even an empty string, and false for numbers, booleans, arrays, objects and null.
It is a string, because it is written in quotes. is_string('123') returns true and is_int('123') returns false. Use is_numeric() to test whether the text looks like a number.
Yes. An empty string is still a value of type string, so is_string('') returns true. Use strlen() or empty() to check for no content.
No. null is a separate type, so is_string(null) returns false. This is true even though echo prints null as nothing.
Use strval($value) or the cast (string) $value. After the conversion, is_string() returns true for the result.

Next: isset()

isset() checks whether one or more variables are set and are not null. It looks like a fun.

isset() 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