Select Page

What is the purpose of the & Ampersand in front of PHP Variables

What this does it creates a reference to the original variable and thus does not copy the value of the original variable.

$original_var = 500;

&$ref_var = $original_var;

//This will print the value 500
echo  $ref_var;

//Now we change the value of the original variable to 600
$original_var = 600;

//Now we print the value of the reference variable, and the reference variable takes the value of the original variable
echo  $ref_var;  //This will print 600

  • You can also reference objects
  • Referencing variables come in handy when trying to accomplish two task at once

NOTEPHP 5 currently supports this

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.