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. For example:

<?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.

<?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";
}

Watch a course Learn object oriented PHP

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.

<?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";
}