Sunday, November 17, 2024
Google search engine
HomeLanguagesPHP is_scalar() Function

PHP is_scalar() Function

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 value
var_dump(is_scalar($var2));
 
$var3 = 5.6; // double value
var_dump(is_scalar($var3));
 
$var4 = "Abc3462"; // string value
var_dump(is_scalar($var4));
 
$var5 = array(1, 2, 3); // array value
var_dump(is_scalar($var5));
 
$var6 = new stdClass; // object value
var_dump(is_scalar($var6));
 
$var7 = tmpfile(); // resource value
var_dump(is_scalar($var7));
?>


Output:

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

Reference: http://php.net/manual/en/function.is-scalar.php

RELATED ARTICLES

Most Popular

Recent Comments