PHP_scalar() function is an inbuilt function in PHP and is used to check whether a variable is a scalar or not.
Syntax:
bool is_scalar ( $var )
Parameter: This function accepts a single parameter as shown in the above syntax and described below.
- $var: Variable to check if it is a scalar or not.
Return Value: It returns TRUE when $var is scalar, otherwise it returns FALSE.Â
Note:
- Variables which contain boolean, double, integer or string types are scalar.
- Array, object, and resource are not scalar.
- is_scalar() does not consider NULL to be scalar.
Below program illustrate the is_scalar() function in PHP:
Example:
php
<?phpÂ
// PHP code to demonstrate the working of// is_scalar() functionÂ
$var1 = true; // boolean value var_dump(is_scalar($var1));Â
$var2 = 3; // integer valuevar_dump(is_scalar($var2));Â
$var3 = 5.6; // double valuevar_dump(is_scalar($var3));Â
$var4 = "Abc3462"; // string valuevar_dump(is_scalar($var4));Â
$var5 = array(1, 2, 3); // array valuevar_dump(is_scalar($var5));Â
$var6 = new stdClass; // object valuevar_dump(is_scalar($var6));Â
$var7 = tmpfile(); // resource valuevar_dump(is_scalar($var7));?> |
bool(true) bool(true) bool(true) bool(true) bool(false) bool(false) bool(false)
