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

W

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

About the author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

About Author

Ian Carnaghan

I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment.

Follow Me