Trying to figure out how to do the equivalent of something I did in javascript but in php. But I'm not sure of the operators to do it. In javascript I wanted to see if a particular parameter being passed was either an object or array.. and if not then was it a string/int and what I did was something like
if (str instanceof Array || str instanceof Object) { //code } else { //code }
anyone know of the equivalent to this for php?
The is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.
The is_object() function checks whether a variable is an object. This function returns true (1) if the variable is an object, otherwise it returns false/nothing.
Let's explain what is an object and associative array in PHP? An object is an instance of a class meaning that from one class you can create many objects. It is simply a specimen of a class and has memory allocated. While on the other hand an array which consists of string as an index is called associative array.
In Java programming language, arrays are objects which are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array.
Use is_array
to check if a variable is an array, and similarly, use is_object
to check if a variable is an object.
Try to use:
if (!is_scalar($var)) { // Varible is object or array }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With