PHP - return value by reference in function

To return a value by reference, both declare the function with an & before its name and when assigning the returned value to a variable:
1:  <?php  
2:       $names = array("A","B","C");  
3:       function &findOne($n){  
4:            global $names;  
5:            return $names[$n];       
6:       }  
7:       $person = &findOne(1);  
8:       $person = "D";  
9:       // the result will be "D"  
10:       echo $names[1];  
11:  ?>  

No comments:

Post a Comment