Nov 182010
Is PHP pass by reference or pass by value?
Answer:
PHP is pass by value, see the code below:
<?php
class Dog {
public $name;
public function __construct($name) {
$this->name = $name;
}
}
function bar($dog) {
$dog = new Dog("bar");
echo $dog->name;
}
$d = new Dog("foo");
echo $d->name;
bar($d);
echo $d->name;
If PHP is pass by reference, the last echo statement will print out "bar" instead of "foo", which is not true.