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
var_dump
to display the variable as is
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$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;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
var_dump($var01a);
echo '<br><br>';
var_dump($var01b);
echo '<br><br>';
var_dump($var01c);
echo '<br><br>';
var_dump($var01d);
echo '<br><br>';
var_dump($var01e);
echo '<br><br>';
var_dump($var01f);
echo '<br><br>';
var_dump($var01g);
echo '<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
float(2.718281828459)
float(1.6E-19)
array(2) {
["c"]=>
int(299792458)
["G"]=>
float(6.67428E-11)
}
NULL
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is a STRING
Using only one statement for the if loop,
the result for some variables
can be displayed wrongly
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$var02a = $_SERVER['REMOTE_ADDR'];
$var02b = $_SERVER['HTTP_USER_AGENT'];
$var02c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var02d = M_E;
$var02e = 1.6e-19;
$var02f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var02g = NULL;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02a))
// statement 1
var_dump($var02a); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02b))
// statement 1
var_dump($var02b); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02c))
// statement 1
var_dump($var02c); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02d))
// statement 1
var_dump($var02d); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02e))
// statement 1
var_dump($var02e); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02f))
// statement 1
var_dump($var02f); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02g))
// statement 1
var_dump($var02g); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var02g))
// statement 1
var_dump($var02g); echo '<br>is a STRING<br><br>';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
RESULT
string(3) "::1"
is a STRING
string(78) "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"
is a STRING
string(35) "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3"
is a STRING
is a STRING
is a STRING
is a STRING
is a STRING
is a STRING
As you can see in the results above, using only one statment for the if loop, the result for some variables can be displayed wrongly.
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - -
This is a simple example using
if -> conditional evaluation
to display if the variable
is a STRING
- - - - - - - - - - - - - - - - - - - - - - - */
$var03a = $_SERVER['REMOTE_ADDR'];
$var03b = $_SERVER['HTTP_USER_AGENT'];
$var03c = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$var03d = M_E;
$var03e = 1.6e-19;
$var03f = [ 'c' => 299792458, 'G' => 6.67428E-11 ];
$var03g = NULL;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03a))
// statement 1
{
echo $var03a . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump(var03a);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03b))
// statement 1
{
echo $var03b . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump(var03b);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03c))
// statement 1
{
echo $var03c . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump(var03c);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03d))
// statement 1
{
echo $var03d . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump($var03d);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03e))
// statement 1
{
echo $var03e . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump($var03e);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03f))
// statement 1
{
echo $var03f . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump($var03f);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if(is_string($var03g))
// statement 1
{
echo $var03g . '<br>is a STRING<br><br>';
}
else
// statement 2
{
var_dump($var03g);
echo '<br>is NOT a STRING<br><br>';
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
?>
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
There is a simpler way of obtaining whether a variable is STRING or not.
This will be seen the moment we study the user-defined functions.
EXERCISE
<?php
$val04a = PHP_INT_MAX;
$tst04a = is_string($val04a);
if($tst04a == true)
{
echo '"' . $val04a . '"<br>is a STRING<br><br>';
}
else
{
var_dump($val04a);
echo '<br>is NOT a STRING<br><br>';
}
$val04b = '123456';
$tst04b = is_string($val04b);
if($tst04b == true)
{
echo '"' . $val04b . '"<br>is a STRING<br><br>';
}
else
{
var_dump($val04b);
echo '<br>is NOT a STRING<br><br>';
}
?>
RESULT
int(9223372036854775807)
is NOT a STRING
"123456"
is a STRING
There is a simpler way of obtaining whether a variable is STRING or not.
This will be seen the moment we study the user-defined functions.