• PHP Video Tutorials

PHP - is_scalar() Function



Definition and Usage

The function is_scalar() whether a variable is a scalar. Scalar variables are those containing an int, float, string or bool. Types array, object and resource are not scalar.

Syntax

bool is_scalar ( mixed $value )

Parameters

Sr.No Parameter & Description
1

value

The variable being evaluated.

Return Values

This function returns true if value is a scalar, false otherwise. This function does not consider NULL to be scalar.

Dependencies

PHP 4.0.5 and above

Example

Following example demonstrates use of function is_scalar() −

<?php
   $a = "Tutorialspoint";
   echo "a is ".( is_scalar($a)? 'scalar' : 'not scalar') . "<br>";

   $b = 0;
   echo "b is ".( is_scalar($b)? 'scalar' : 'not scalar') . "<br>";

   $c = 40;
   echo "c is ".( is_scalar($c)? 'scalar' : 'not scalar') . "<br>";

   $d = NULL;
   echo "d is ".( is_scalar($d)? 'scalar' : 'not scalar') . "<br>";

   $e = array("a", "b", "c");
   echo "e is ".( is_scalar($e)? 'scalar' : 'not scalar') . "<br>";

   $f = 3.1416;
   echo "f is ".( is_scalar($f)? 'scalar' : 'not scalar') . "<br>";

   $g = new stdClass();
   echo "g is ".( is_scalar($g)? 'scalar' : 'not scalar') . "<br>";
?>

Output

This will produce following result −

a is scalar
b is scalar
c is scalar
d is not scalar
e is not scalar
f is scalar
g is not scalar
php_variable_handling_functions.htm
Advertisements