is_scalar


php128 apg

CHECKS whether a VARIABLE is SCALAR.

This function returns a BOOLEAN value when it tests the following VARIABLE types:

STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.


SCALAR

is a VARIABLE which consists of one of the types:

STRING, INTEGER, FLOAT or BOOLEAN.



<?php

bool is_scalar 
mix $var );


where,

$var VARIABLE to test

?>

$var


The VARIABLE to test.





To make it easier to recognize whether a variable is SCALAR, we will use a user-defined function that can be seen below:



<?php

   
function isscalar($arg
            { 
            
$type is_scalar($arg); 
            { 
                        if(
$type == TRUE
                        
// statement 1 
                        
{
                    
var_dump($arg);
                        echo  
'<br>( is SCALAR )<br>'
                        }
                        else 
                        
// statement 2 
                        
{
                    
var_dump($arg);
                        echo 
'<br>( is NOT SCALAR )<br>'
                        }
                        } 
                        }
                        
where,

$arg ARGUMENT to test

?>
 

  1 EXERCISE   

<?php

function isscalar($var

$type is_scalar($var); 

if(
$type == TRUE
// statement 1 
{
var_dump($var);
echo  
'<br>( is SCALAR )<br><br>'
}
else 
// statement 2 
{
var_dump($var);
echo 
'<br>( is NOT SCALAR )<br><br>'
}

}

$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;

$var01h true;

isscalar($var01a);
isscalar($var01d);
isscalar($var01e);
isscalar($var01f);
isscalar($var01g);
isscalar($var01h);

?>

 RESULT   

string(3) "::1"
( is SCALAR )

float(2.718281828459)
( is SCALAR )

float(1.6E-19)
( is SCALAR )


array(2) { ["c"]=> int(299792458) ["G"]=> float(6.67428E-11) }
( is NOT SCALAR )

NULL
( is NOT SCALAR )


bool(true)
( is SCALAR )


Note that some floating point values ​​are presented differently in PHP versions 7.4.XX and 8.0.XX