Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

up vote 13 down vote accepted

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

share|improve this answer
 
yea, its funny.. no sooner than I typed it out.. i remembered is_array then I looked up to see if theres similar for objects.. im super clever like that today apparently, thanks. By the way is there by chance anything like this for JSON cause I'd like to throw that in my mix so if it is JSON i can decode/encode accordingly to work with it –  chris Jun 8 '12 at 23:27
2  
@chris: I suppose you can try to json_decode it, and if it fails, it's not JSON. –  minitech Jun 8 '12 at 23:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.