is_stringFINDS if a given variable is a STRING.
This function returns a BOOLEAN value when it tests the following variable types:
STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.
This functions returns TRUE wheather $var is a STRING or FALSE in not.
To visualize the result, in this tutorial, we use var_dump associated with an if loop structure as a conditional evaluation framework.
<?php
bool is_string ( mix $var )
where,
$var = The variable to be checked
?>
$var
The variable to be checked.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
a function: isstring -> user-defined
to display if the variable
is a STRING
- - - - - - - - - - - - - - - - - - - - - - - */
$var01a = $_SERVER['REMOTE_ADDR'];
$var01b = $_SERVER['HTTP_USER_AGENT'];
$var01c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var01d = M_E;
$var01e = 1.6e-19;
$var01f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var01g = NULL;
function isstring($var)
{
if(is_string($var))
echo $var . '<br>is a STRING!<br><br>';
else
{
var_dump($var);
echo '<br>is NOT a STRING!<br><br>';
}
}
isstring($var01a);
isstring($var01b);
isstring($var01c);
isstring($var01d);
isstring($var01e);
isstring($var01f);
isstring($var01g);
?>
RESULT
::1
is a STRING!
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0
is a STRING!
pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
is a STRING!
float(2.718281828459)
is NOT a STRING!
float(1.6E-19)
is NOT a STRING!
array(2) {
["c"]=>
int(299792458)
["G"]=>
float(6.67428E-11)
}
is NOT a STRING!
NULL
is NOT a STRING!
EXERCISE
<?php
echo "Testing is_string() with valid string values:<br>";
// different valid strings
/* string created using Heredoc (<<<) */
$heredoc_string = <<<EOT
This is string defined
using heredoc.
EOT;
/* heredoc string with only numerics */
$heredoc_numeric_string = <<<EOT
123456 3993
4849 string
EOT;
/* null heardoc string */
$heredoc_empty_string = <<<EOT
EOT;
$heredoc_null_string = <<<EOT
NULL
EOT;
$strings = array(
"",
" ",
'',
' ',
"string",
'string',
"NULL",
'null',
"FALSE",
'true',
"\x0b",
"\0",
'\0',
'\060',
"\070",
"0x55F",
"055",
"@#$#$%%$^^$%^%^$^&",
$heredoc_string,
$heredoc_numeric_string,
$heredoc_empty_string,
$heredoc_null_string
);
/* loop to check that is_string()
recognizes different
strings, expected output bool(true) */
$loop_counter = 1;
foreach ($strings as $string ) {
echo "<br><br>Iteration: $loop_counter<br>";
$loop_counter++;
var_dump( is_string($string) );
}
echo "<br><br><br>Testing is_string() on non string values:<br>";
// get a resource type variable
$fp = fopen (__FILE__, "r");
$dfp = opendir ( __DIR__ );
// unset vars
$unset_string1 = "string";
$unset_string2 = 'string';
$unset_heredoc = <<<EOT
this is heredoc string
EOT;
// unset the vars
unset($unset_string1, $unset_string2, $unset_heredoc);
// other types in a array
$not_strings = array (
/* integers */
0,
1,
-1,
-0,
543915,
-5322,
0x0,
0x1,
0x55F,
-0xCCF,
0123,
-0654,
00,
01,
/* floats */
0.0,
1.0,
-1.0,
10.0000000000000000005,
.5e6,
-.5E7,
.5E+8,
-.5e+90,
1e5,
-1e5,
1E5,
-1E7,
/* objects */
new stdclass,
/* resources */
$fp,
$dfp,
/* arrays */
array(),
array(0),
array(1),
array(NULL),
array(null),
array("string"),
array(true),
array(TRUE),
array(false),
array(FALSE),
array(1,2,3,4),
array(1 => "One", "two" => 2),
/* undefined and unset vars */
@$unset_string1,
@$unset_string2,
@$unset_heredoc,
@$undefined_var
);
/* loop through the $not_strings to see working of
is_string() on non string types,
expected output bool(false) */
$loop_counter = 1;
foreach ($not_strings as $type ) {
echo "<br><br>Iteration: $loop_counter<br>";
$loop_counter++;
var_dump( is_string($type) );
}
// close the resources used
fclose($fp);
closedir($dfp);
?>