John Davidson

php is variable an array or object

0 comments
Message:


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?



My Answer:
In PHP, you can use the `is_array()` and `is_object()` functions to check if a variable is an array or an object, respectively. Here is the equivalent code in PHP:

php
if (is_array($var) || is_object($var)) {
// code
} else {
// code
}


This code will check if the variable `$var` is either an array or an object. If it is, the first block of code will be executed, otherwise the second block of code will be executed.

Rate this post

3 of 5 based on 3175 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us