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);
Call
What 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
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 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.
null is not a string. It is not even an empty string, so the result is false.
An array is not a string. is_string() never looks inside it.
strval(5) returns the text '5'. The converted value is a string, so the result is true.
(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;
Output
HELLO!
Please pass a string
How It Works
The function has no type declaration. Callers can send any value, so we check it ourselves.
The guard uses !is_string(). If the value is not text, the function returns early.
'hello' passes the check. strtoupper() makes it HELLO and the dot adds an exclamation mark.
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.