I have never seen this before and wanted to know if someone could explain what it does? I’m pretty advanced with PHP but have never used this kind of statement.
Anyone know?
$somevar = $anothervar = array ();
Thanks
I have never seen this before and wanted to know if someone could explain what it does? I’m pretty advanced with PHP but have never used this kind of statement.
Anyone know?
$somevar = $anothervar = array ();
Thanks
Perhaps I should have said inconsistent, but it’s one of the reasons I consider $foo = $bar = array() messy.
Only if you misunderstand how the language works. It is identically equivalent to the following and works exactly as you’d expect the following to work:
$bar = new stdClass;
$foo = $bar;
It would only give unexpected results if you expected it to be equivalent to
$bar = new stdClass;
$foo = new stdClass;
It is a fairly standard way to assign the same value to a lot of different variables when you are initialising them. You will find the same thing in a lot of different languages.
Be wary though…
$foo = $bar = new stdClass;
will have unexpected results!
Thanks for the reply!
Makes sense, I agree sounds a bit iffy. I wont be using it, just wanted to know what it was as I have never used before
In PHP, the left operand gets set to the value of the expression on the right. So, $somvar is assigned the value of $anothervar, which is assigned an array.
It’s a quick (but dirty IMHO) way to declare the two variables as an array.