PHP | is_scalar() Function

The is_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 above syntax and 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
?>

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.