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

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

PDF Print E-mail

Question: What is the purpose of the & Ampersand in front of PHP Variables? For example:  &$variable;

 

Answer:

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

 

NOTE: PHP 5 currently supports this

 

 

+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!