03-01-2009, 07:56 PM
|
#6 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Salathe
No, arrays are passed by value unless explicitly passed by reference (the same way as objects were handled in PHP4). Example:
PHP Code:
$one = array('a' => 'apple', 'b' => 'banana', 'c' => 'car');
$two = $one;
$three =& $one;
$one['b'] = 'ball';
var_dump($one, $two, $three);
|
Thanks Sal:
I got this output and I understand it:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(4) "ball"
["c"]=>
string(3) "car"
}
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(3) "car"
}
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(4) "ball"
["c"]=>
string(3) "car"
}
I thought it was pass by value until the other day I wrote a quick script that used array_pop and I got confusing results.
So I thought maybe it might actually be pass by reference...
But I just wrote another script and now it works as advertised. I wish I could remember the other script so I can see where it went wrong.
Quick question about proper syntax. Both of these below worked, and I'm sure it's just preference but what do you think is the best syntax to use?
$three =& $one;
$three = &$one;
The only reason why I would say the second example is that when you are passing by reference to a function it would look more like the second one:
test_function(&$variable)
thoughts?
|
|
|
|