Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
is_array
communitycreator

What is the is_array 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.

The is_array method is a built-in function in PHP used to check whether a variable is an array.

Syntax

is_array(mixed $variable_to_test): bool

Parameter

The function takes in one parameter, variable_to_test.

Return value

The is_array method returns true if the passed argument is an array; otherwise, it returns false.

Code

<?
$arr=array('A', 'B', 'C');
print_r($arr);
echo is_array($arr) ? ' is an Array' : ' is not an Array';
echo "\n\n-----------\n";
$arr = array('odd' => array(3, 5), 'even' => array(2, 4, 6));
print_r($arr);
echo is_array($arr) ? ' is an Array' : ' is not an Array';
echo "\n\n-----------\n";
$num = 67.099;
print($num);
echo is_array($num) ? ' is an Array' : ' is not an Array';
echo "\n\n-----------\n";
$str= "hello";
print($str);
echo is_array($str) ? ' is an Array' : ' is not an Array';
?>

In the code above, we call the is_array method on $arr.

$arr=array('A', 'B', 'C');
is_array($arr)

For the variable $arr, the is_array method returns true because $arr is an array.


$arr = array(
              'odd' => array(3,5), 
              'even' => array(2,4,6)
            );
is_array($arr);

For the value of $arr above, the is_array method returns true because $arr is a multidimensional array.


$num = 67.099;
is_array($num);

For the above value of $num, the is_array method returns false because $num is not an array.


$str= "hello";
is_array($str);

For the value of $num, the is_array method returns false because $num is a string, not an array.

RELATED TAGS

php
is_array
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.

Keep Exploring