PHP is_string() function

The PHP is_string() function is used to find whether a certain value is a string type.

The function syntax is as follows:

is_string(mixed $value): bool

This function accepts a value and returns true when that value is string type. Otherwise, it returns false.

Here are some examples of calling the function:

// 👇 pass variable to is_string
$int = 7;
var_dump(is_string($int)); // bool(false)

$str = "Hi";
var_dump(is_string($str)); // bool(true)

// 👇 pass direct value to is_string

var_dump(is_string([1, 3])); // bool(false)

var_dump(is_string(["H", "A"])); // bool(false)

var_dump(is_string(NULL)); // bool(false)

var_dump(is_string("789")); // bool(true)

Use the is_string() function when you need to check whether a variable is a string.

The returned boolean value can be used in an if-else conditional statement 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.