Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
is_scalar
communitycreator

What is the is_scalar method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The is_scalar method checks whether the variable is a scalar or not.


int, float, string, and bool values are considered scalar values. Other values like an array, object, resources, or NULL are not scalar.

Syntax


is_scalar(mixed $value): bool

The is_scalar method returns true if the passed value is scalar. Otherwise, it returns false.

Code

<?php
$test = true;
printIsScalar($test);
$test = "str";
printIsScalar($test);
$test = 0;
printIsScalar($test);
$test = [1];
printIsScalar($test);
$test = null;
printIsScalar($test);
function printIsScalar($test){
echo "The Value is: ";
var_dump($test);
echo "Is Scalar: ";
var_dump(is_scalar($test));
echo "---------\n\n";
}
?>

Explanation

In the code above:

  • We use the is_scalar method to check if a variable is a scalar or not.

  • For the values true, "str", and 0, the is_scalar method returns true.

  • For the values [1](array) and null, the is_scalar method returns false because they do not fall under scalar type values.

RELATED TAGS

php
is_scalar
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring