Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing by reference assignment php [duplicate]

Tags:

php

As per the php code below, the output is

1 . 1 
2 . 2 
3 . 3 

I understand &$ref is passing by reference. but its like after the assignment($row = &$ref;) everywhere whenever 'row' changes the value, 'ref' changes as the same value as 'row' too. really confusing. Seems like that = is not only assign the right value to the left. Can someone please verify this?

<?php
$ref = 0;
$row = &$ref;
foreach (array(1, 2, 3) as $row) {
    print "$row . $ref \n" ;
}
echo $ref; 
?>
like image 744
user2381130 Avatar asked May 01 '15 15:05

user2381130


1 Answers

When you do $row = &$ref;

It means: the same variable content with another name. That is, the same, not a copy. What you do in $ref, it will be made in $row ... and vice versa.

like image 163
Alejandro Salamanca Mazuelo Avatar answered Oct 16 '22 21:10

Alejandro Salamanca Mazuelo