How to check if variable is array?... or something array-like
In PHP, you can use the "is_array" function to check if a variable is an array.
In PHP, you can use the "is_array" function to check if a variable is an array. For example:
Example of using the "is_array" function to check if a variable is an array in PHP
<?php
$example_array = [1, 2, 3];
if (is_array($example_array)) {
echo "example_array is an array";
} else {
echo "example_array is not an array";
}If you want to check if a variable is something array-like, you could use the "is_iterable" function, this function checks if the variable is an array or an object that implements the Traversable interface, starting from PHP 7.1.
Example of using the "is_iterable" function to check if a variable is something array-like in PHP
<?php
$example_array = [1, 2, 3];
if (is_iterable($example_array)) {
echo "example_array is an iterable";
} else {
echo "example_array is not an iterable";
}
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
You can also use the instanceof operator to check if the variable is an instance of the ArrayObject or ArrayIterator classes, but it's less recommended.
Example of using the instanceof operator to check if the variable is an instance of the ArrayObject or ArrayIterator classes in PHP
<?php
$example_array = new ArrayObject([1, 2, 3]);
if ($example_array instanceof ArrayObject) {
echo "example_array is an ArrayObject";
} else {
echo "example_array is not an ArrayObject";
}