PHP

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

Written by Ian Carnaghan · 21 sec read >

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

Last Updated On March 22, 2018
Written by 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. Profile

Leave a Reply

avatar
  Subscribe  
Notify of