is_string

Checks whether a variable is of type string, without any type juggling.

Signature
is_string(mixed $value): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if value is of type string, false otherwise.

is_string() is a strict type test with no juggling: it returns true only when the variable's internal type is string. The empty string "" passes, "0" passes, and a 5 MB blob passes. Integers, floats, null and arrays do not.

When to reach for it

Use it where a value could legitimately be several types and the string branch behaves differently — a function that accepts either a class name or an instance, a route handler that takes a callable or a template path, or validation of decoded JSON where a field must be text rather than a number. It is also a sanity check before functions that would otherwise coerce silently, such as strlen() or preg_match().

What trips people up

Data from HTTP is always string-typed. $_POST['age'] is "30", not 30, so is_string() tells you nothing useful about form input — use is_numeric() to ask whether it looks like a number.

Objects implementing Stringable — including anything with __toString() — are objects, not strings, so they fail. If you want "a string or something that becomes one", test is_string($v) || $v instanceof Stringable, which is the same pair the string|Stringable type declaration accepts.

is_string() says nothing about encoding; use mb_check_encoding() if you need valid UTF-8.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Accepting either a class-name string or an object instance in a factory
  • Validating that a JSON field arrived as text and not a number
  • Deciding between treating an argument as a template path or a callable

Frequently asked questions

Does is_string return true for numeric strings like "42"?

Yes. is_string() checks the variable's type, not its content, so "42" is a string. Use is_numeric() to ask whether the text represents a number.

Why does is_string return false for my object with __toString?

Because the variable still holds an object, not a string. Test $v instanceof Stringable alongside is_string(), or cast with (string) $v before checking.

Is an empty string still a string?

Yes, is_string("") returns true. If you want to reject blanks, combine it with a length check such as is_string($v) && $v !== ''.

More examples

Accepting a class name or an instance

is_string() distinguishes the lazy form (a class name to instantiate) from an already-built object. Without the check, new $target() on an object would be a fatal error.

Example · php
<?php
class Mailer { public function send(): string { return 'sent'; } }

function resolve(string|object $target): object {
    if (is_string($target)) {
        return new $target();
    }
    return $target;
}

echo resolve('Mailer')->send(), "\n";
echo resolve(new Mailer())->send(), "\n";
echo get_class(resolve('Mailer')), "\n";

Validating a decoded JSON payload

JSON preserves types, so age arrives as a real int and fails is_string(). The rules array shows how the is_* family works as plain callbacks for schema-style validation.

Example · php
<?php
$payload = json_decode('{"name":"Ada","age":36,"tags":["a"]}', true);
$rules = ['name' => 'is_string', 'age' => 'is_int', 'tags' => 'is_array'];

foreach ($rules as $field => $check) {
    $ok = array_key_exists($field, $payload) && $check($payload[$field]);
    echo str_pad($field, 6), $ok ? "ok\n" : "invalid\n";
}

var_dump(is_string($payload['age']));

Discussion

  • Be the first to comment on this lesson.