views:

174

answers:

2

Hi guys, I was wondering if i could assign values to a variable inside an IF statement. My code is as follows:

<?php
    if ((count($newArray) = array("hello", "world")) == 0) {
        // do something
    }
?>

So basically i want assign the array to the $newArray variable, then count newArray and check to see if it is an empty array.

I know i can do this on several lines but just wondered if i could do it on one line

Thanks M

+1  A: 

Yeah, you can, like so:

if(count($ary = array(1,2,3)))

Doing a var_dump of $ary gives:

array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
notJim
+2  A: 

Try this:

if(count($newArray = array("Hello", "world")) == 0) {
    ....
}

I'd advice against this though, as it makes your code less readable. And highly illogical too as you know that the array in question contains two values. But perhaps you have something else in mind. :)

Tatu Ulmanen