is_scalar()

Description

The is_scalar() function is used to determine if a passed value is a scalar expression. Scalar expressions are those of type integer, float, string, and Boolean. Arrays, Objects, and Resources are not scalar. Also NULL expressions are not considered scalar.

Syntax


bool is_scalar( mixed $expr )

Parameters

$expr
Expression to test.

Return Value

boolean
A Boolean value is returned as follows:

  • true if passed expression is a scalar
  • false if passed expression is not a scalar

Examples

1. Check All Types with is_scalar()

This example creates a variable of each data type and passes it to is_sclar(). The return value of the is_scalar() call is passed to var_dump() to display its type and value. Note that the file data.txt exists in the directory so the call to fopen() on line 8 succeeds.

<?php
$boolVal     = true;
$intVal      = 6;
$floatVal    = 3.14;
$stringVal   = "Hello";
$arrayVal    = array( 1, "two", 3.4 );
$objectVal   = new stdClass;
$resourceVal = fopen("./data.txt", "r");
$nullVal     = null;
 
var_dump( is_scalar( $boolVal ));
var_dump( is_scalar( $intVal ));
var_dump( is_scalar( $floatVal ));
var_dump( is_scalar( $stringVal ));
var_dump( is_scalar( $arrayVal ));
var_dump( is_scalar( $objectVal ));
var_dump( is_scalar( $resourceVal ));
var_dump( is_scalar( $nullVal ));
 
fclose($resourceVal);
?>

The output from the program is shown below. Note that the Boolean, integer, float, and string variables are all scalar, while the array, object, resource, and NULL are not.

bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)

See Also

is_bool
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
var_dump

References