PHP is_string() function

Posted on Aug 15, 2022

Check if a value is a string type or not using PHP is_string()

Photo from Unsplash

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 👍

Level up your programming skills

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

No spam. Unsubscribe anytime.