W3docs

is_string()

The is_string() function is a built-in function in PHP that checks whether a variable is a string or not. A string is a data type that represents a sequence of

Introduction

is_string() is a built-in PHP function that tells you whether a value is of the string type. A string is a data type that holds a sequence of characters, such as "hello" or "42".

Because PHP is loosely typed, a value's type often depends on where it came from — form input, a database, a JSON payload, or a function's return value. is_string() lets you confirm a value really is a string before you call string-only operations on it (like strtoupper() or substr()), so you can avoid type errors and surprising results.

This page covers the syntax, what is_string() returns for each PHP type, the common "numeric string" gotcha, and how it differs from related functions like is_numeric() and gettype().

Syntax

is_string(mixed $value): bool
  • $value — the variable or expression to test.
  • Returnstrue if $value is a string, false for every other type.

is_string() checks the type, not the content. It only ever inspects one value at a time and never converts it.

Basic example

php— editable, runs on the server

$var1 is a string, so is_string() returns true; $var2 is a float, so it returns false.

Tip: Use var_dump() instead of echo when testing boolean results. echo true prints 1, but echo false prints nothing at all — which makes a false result easy to miss. var_dump() shows bool(true) / bool(false) explicitly.

What is_string() returns for each type

Only actual strings pass the check — numbers, booleans, null, and arrays all return false, even when they look string-like.

<?php
var_dump(is_string("hello"));      // bool(true)
var_dump(is_string("42"));         // bool(true)  — a digit string is still a string
var_dump(is_string(42));           // bool(false) — integer
var_dump(is_string(3.14));         // bool(false) — float
var_dump(is_string(true));         // bool(false) — boolean
var_dump(is_string(null));         // bool(false) — null
var_dump(is_string(['a', 'b']));   // bool(false) — array
?>

The key takeaway: is_string("42") is true. The quotes make it a string of digits, not a number.

A practical use: guard before string operations

A typical use is validating a value before running string functions on it, so untrusted or mixed-type input can't trigger a TypeError.

<?php
function shout($value): string {
    if (!is_string($value)) {
        return "Not a string";
    }
    return strtoupper($value) . "!";
}

echo shout("hello") . "\n"; // HELLO!
echo shout(123) . "\n";     // Not a string
?>

is_string() vs. is_numeric()

These two are often confused because numeric strings sit between them. is_string() asks "is this the string type?", while is_numeric() asks "does this value represent a number?" — and a numeric string answers yes to both.

<?php
$value = "42";

var_dump(is_string($value));  // bool(true)  — it IS a string
var_dump(is_numeric($value)); // bool(true)  — and it looks like a number
?>

If you specifically need an integer or a float type rather than a string, use is_int() or is_float(). To convert a value to a string instead of just checking it, see settype() or strval().

Conclusion

is_string() is a simple, reliable type check: it returns true only for values that are genuinely of the string type and false for everything else. Reach for it whenever you need to be sure a value is a string before applying string operations — for example when handling user input, API responses, or any data whose type isn't guaranteed. Remember the two gotchas: a quoted number like "42" is a string, and you should inspect results with var_dump() rather than echo so a false doesn't disappear. For related checks, explore is_numeric(), is_int(), is_array(), and gettype().

Practice

Practice
Which of the following statements about 'is_string' in PHP are true?
Which of the following statements about 'is_string' in PHP are true?
Was this page helpful?