PHP is_string() Function

What is PHP is_string() Function?

If you want to check whether a value is of string type or not, use is_string() function. Instead of using value directly, you can use a variable to check its type too.

Syntax:

is_string(value)

Parameters:

The Function has 1 parameter which is required-

value (Required): It is the value or variable whose type to be evaluated. This parameter can accept any type of value/variable. Check example 1.

Return Values:

The function returns-

  • TRUE – if the value is of string type.
  • FALSE – if the value is not of type string.

Check example 2.

How to use is_string() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result the conditional statement returns, your program flow progresses. Have a look at the code-

<?php
$Username = $_POST['Username'];
if(is_string($Username)){
    echo "Username is: " . $Username;
}else{
    echo "Username must be a string.";
}
?>

Examples:

Example 1: is_string() can check type of a value or a variable-

<?php
var_dump(is_string("PHP for Beginners"));
echo "<br />";
var_dump(is_string(35));
echo "<br />";
$course = "PHP for Beginners";
$lesson = 35;
var_dump(is_string($course));
echo "<br />";
var_dump(is_string($lesson));
?>

Output:

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

Example 2: is_string() function examples-

<?php
var_dump(is_string(NULL));
echo "<br />";
var_dump(is_string(FALSE));
echo "<br />";
var_dump(is_string(99));
echo "<br />";
var_dump(is_string(" "));
echo "<br />";
var_dump(is_string(""));
echo "<br />";
$arr = [];
var_dump(is_string($arr));
echo "<br />";
class Language {}
$php = new Language();
var_dump(is_string($php));
?>

Output:

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

Why type checking is important?

Type checking is crucial part of any project. It can break your code if not done correctly.

  • Users may provide different type of data than they are asked to provide. For example, in the name field, they may add numeric value. Though, you may apply client-side validation to prevent this error, you must implement server-side validation too with PHP.
  • As PHP can automatically change data type (Type Juggling) of a variable, you need to be careful situations like this when type conversion may occur. In the following code snippet, the string “35” converts to number after the addition.
    <?php
    $string = "35";
    $lesson = 35;
    echo $newString = $string + $lesson;
    ?>
    

Practical Usages of is_string() Function:

  • Suppose you declared a table column named “Status” which only accepts characters. Before inserting data in this column, you can check the data type with the is_string() function. Check the code snippet below-
    <?php
    $Status = $_POST['Status'];
    if(is_string($Status)){
        mysqli_query($connection, "INSERT INTO `product` (`Status`, ...) 
                            VALUES ('$Status',...)");
    }else{
        echo "Status is not a string.";
    }
    ?>
    
  • Suppose users are instructed to add only characters in the “Name” field. But, one user added number instead of string. Before further processing this data, you should at first check its type with is_string() function.
    <?php
    $Name = $_POST['Name'];
    if(is_string($Name)){
        echo "Name is: " . $Name;
    }else{
        echo "You entered non-string value in the Name.";
    }
    ?>
    

Notes on is_string() Function:

You can create strings in PHP in 4 ways- a. Single quote, b. Double quote, c. Heredoc syntax, and Nowdoc syntax. The function can check type of a value/variable that is created in any of these methods. Look at the following code-

<?php
echo "Is it string type variable? "; var_dump(is_string('Did you check HTML for Beginners?'));
echo "<br />";
echo "Is it string type variable? "; var_dump(is_string('Did you check JavaScript for Beginners?'));
echo "<br />";
$var = <<<EOD
Did you check "PHP for Beginners?"
EOD;
echo "Is it string type variable? "; var_dump(is_string($var));
echo "<br />";
$var = <<<'EOD'
Did you check "CSS for Beginners?";
EOD;
echo "Is it string type variable? "; var_dump(is_string($var));
?>

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_string() Function

The correct data type can make or break your code. The function gives you the ability to check whether a variable is a string type variable. is_string() is one of the useful built-in variable-handling function.

Reference:

https://www.php.net/manual/en/function.is-string.php