Thursday, January 18, 2007

References

The reference operator & was added to PHP in version 4. References are useful when you want to refer to a variable by using a name other than its original one.

$x = 3 ;
$y = &$x ;

The second statement creates a reference for $x named as $y. After now we can access the variable by using its original one or the reference. As we assign a value to one of them, the other has the same value. In fact a reference has not a memory for the value. It only references the original variable.

$x = 5 ;

$x and $y are both 5 after this statement. The reference operator may remind you pointers in the C language. The function is similar but you can't assign the address of a reference manually.