Difference between passing objects by reference and without in PHP

Quite often I here, that people don't understand, how to use variables by reference. PHP is not an exception. This will be a very short post in which I will try to make it clear.
First of all we will define simple object class A.

class A
{
	protected $foo;
	
	public function __toString()
	{
		return $this->getFoo();
	}
	
	public function setFoo( $foo )
	{
		$this->foo = $foo;
	}
	
	public function getFoo()
	{
		return $this->foo;
	}
}

First I will show how everything works without references.
(Note! Objects in PHP and in most programming languages are already passed by reference)

Passing without ref.

$A      = new A();
$fooVar = '';

function changeFoo( $value )
{
	$text = '';
	
	if ( $value instanceof A )
	{
		$text = 'Object';
		$value->setFoo( 'bar' );
	}
	else
	{
		$text  = 'Simple string';
		$value = 'bar';
	}
	
	echo $text . ' in the function:<br />';
	echo "Value: {$value}<br /><br />";
}

changeFoo( $A );
echo 'Object out of the function:<br />';
echo "Value: {$A}<br /><br />";

changeFoo( $fooVar );
echo 'Simple string out of the function:<br />';
echo "Value: {$fooVar}<br /><br />";

Output

Passing without ref.

Object in the function:
Value: bar

Object out of the function:
Value: bar

Simple string in the function:
Value: bar

Simple string out of the function:
Value: 

As you can see the last line doesn't have a value. This is because when we passed simple string value to a function a local copy of this value was created inside a function. Therefore when we output value of simple string variable in function we see it, but outside of it, it is still an empty string.

Now we will look at how we can pass a value by refence.

Passing by ref.

$A      = new A();
$fooVar = '';

function changeFooWithRef( &$value )
{
	$text = '';
	
	if ( $value instanceof A )
	{
		$text = 'Object';
		$value->setFoo( 'bar' );
	}
	else
	{
		$text  = 'Simple string';
		$value = 'bar';
	}
	
	echo $text . ' in the function:<br />';
	echo "Value: {$value}<br /><br />";
}

changeFooWithRef( $A );
echo 'Object out of the function:<br />';
echo "Value: {$A}<br /><br />";

changeFooWithRef( $fooVar );
echo 'Simple string out of the function:<br />';
echo "Value: {$fooVar}<br /><br />";

Output

Passing by ref.

Object in the function:
Value: bar

Object out of the function:
Value: bar

Simple string in the function:
Value: bar

Simple string out of the function:
Value: bar

Yes, at last it works. So as you can see in this example function changeFooWithRef( &$value ) we have a ampersand symbol in front of argument. It means, that when we call this function and pass variable into it, we are actually passing address at which this variable is stored in memory. So when we now are changing simple string variable value, we are actually working with original value, not a copy of it.

I hope this explanation helps to those young minds who are lost, but are trying.
If there are any questions left, feel free to ask in comments.
Cheers!