PHP Function Reference

PHP is_string() Function



The PHP is_string() function checks whether a variable is of type string. The function returns true if the variable is a string, otherwise it returns false.

Syntax

is_string(variable)

Parameters

variable Required. Specify the variable being evaluated.

Return Value

Returns true if variable is a string, false otherwise.

Example:

The example below shows the usage of is_string() function.

<?php
var_dump(is_string('10'));    //returns: bool(true)
var_dump(is_string('10.5'));  //returns: bool(true)
var_dump(is_string('xyz'));   //returns: bool(true)
var_dump(is_string('1e5'));   //returns: bool(true)
var_dump(is_string('true'));  //returns: bool(true)

echo "\n";

var_dump(is_string(10));    //returns: bool(false)
var_dump(is_string(10.5));  //returns: bool(false)
var_dump(is_string(1e5));   //returns: bool(false)
var_dump(is_string(true));  //returns: bool(false)
?>

The output of the above code will be:

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

bool(false)
bool(false)
bool(false)
bool(false)

Example:

Consider one more example where this function is used to check all elements of an array whether they are of type string or not.

<?php
$Arr = array(10, "10", 10.5, "10.5", null, 
            false, "true", 1e3, "1e3"); 

foreach ($Arr as $value) {
  echo "is_string(";
  var_export($value);
  echo ") = ";
  var_dump(is_string($value));
}
?>

The output of the above code will be:

is_string(10) = bool(false)
is_string('10') = bool(true)
is_string(10.5) = bool(false)
is_string('10.5') = bool(true)
is_string(NULL) = bool(false)
is_string(false) = bool(false)
is_string('true') = bool(true)
is_string(1000.0) = bool(false)
is_string('1e3') = bool(true)

❮ PHP Variable Handling Reference