PHP | is_string() Function

The is_string() function is an inbuilt function in PHP which is used to check whether the given value is a string or not.

Syntax:

bool is_string( mixed $var )

Parameters: This function accepts one parameter as mentioned above and described below:



  • $var: It contains the value that need to check.

Return Value: It returns TRUE if the value of variable is of string type and FALSE otherwise.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$str = "GeeksforGeeks";
  
// Check value of variable is set or not
if(is_string($str)) {
    echo "String";
}
else {
    echo "Not String";
}
  
$arr = array(10, 25.56, 'G', true, 'Geeks');
  
foreach ($arr as $index) {
    if(is_string($index)) {
        echo "\nString";
    }
    else {
        echo "\nNot String";
    }
}
  
?>

chevron_right


Output:

String
Not String
Not String
String
Not String
String

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
$num = 21;
var_dump(is_string($num));
  
$arr = array(
        "a" => "Welcome",
        "b" => 2,
        "c" => "GeeksforGeeks"
    );
  
var_dump(is_string($arr["a"]));
var_dump(is_string($arr["b"]));
var_dump(is_string($arr["c"]));
  
?>

chevron_right


Output:

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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.