Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php is variable an array or object

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?

like image 818
chris Avatar asked Jun 08 '12 23:06

chris


People also ask

Is a variable an array 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.

Is variable object in PHP?

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.

Is PHP an object or array?

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.

Is an array a variable or object?

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.


2 Answers

Use is_array to check if a variable is an array, and similarly, use is_object to check if a variable is an object.

like image 85
Ry- Avatar answered Sep 25 '22 14:09

Ry-


Try to use:

if (!is_scalar($var)) {     // Varible is object or array } 
like image 35
WOLFF Avatar answered Sep 23 '22 14:09

WOLFF