PHP is_string() Function

Quick summary: The PHP is_string() function checks whether a variable is of type string.

PHP is_string() Syntax

is_string(mixed $value): bool
PHP

PHP is_string() Basic examples

var_dump(is_string("hello"));
PHP
Output:
bool(true)

Returns true for strings.

var_dump(is_string(123));
PHP
Output:
bool(false)

Integers are not strings.

PHP is_string() Real-world usage

$name = "John";
if (is_string($name)) {
    echo 'Valid name';
}
PHP
Output:
Valid name

Ensures input is a string.

PHP is_string() Edge cases

var_dump(is_string(null));
PHP
Output:
bool(false)

Null is not a string.

PHP is_string() Common mistakes

Confusing stringable values with strings

Objects with __toString are not strings.

Incorrect
is_string(new DateTime());
Correct
(string) new DateTime();

Cast explicitly if string output is required.

PHP is_string() Frequently Asked Questions

What does is_string() do?

Checks if a variable is a string.

Return type?

Boolean.

Does it modify variable?

No.

Is numeric string a string?

Yes.

Use case?

Type checking.

Handles empty string?

Yes.

Handles null?

No.

Handles objects?

No.

Difference from gettype()?

Returns boolean instead of string.

Performance?

Very fast.

Strict check?

Yes.

Alternative?

gettype().

PHP is_string() Related PHP Functions