Call By Value And Call By Reference In PHP

Call By Value

 
In this method, only values of actual parameters are passing to the function. So there are two addresses stored in memory. Making changes in the passing parameter does not affect the actual parameter.
 
Example
  1. <?php  
  2. function increment($var){  
  3.     $var++;  
  4.     return $var;  
  5. }  
  6. $a = 5;  
  7. $b = increment($a);  
  8. echo $a//Output: 5  
  9. echo $b//Output: 6   
  10. ?>   

Call by Reference

 
In this method, the address of actual parameters is passing to the function. So any change made by function affects actual parameter value.
 
Example
  1. <?php  
  2. function increment(&$var){  
  3.     $var++;  
  4.     return $var;  
  5. }  
  6. $a = 5;  
  7. $b = increment($a);  
  8. echo $a//Output: 6  
  9. echo $b//Output: 6   
  10. ?>