Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

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.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>