Jump to content

Trying to understand passing by reference


jGiblets

Recommended Posts

Hi all, I'm trying to understand passing by reference.  Here is a copy of the code and the results:

<?php

        $a1 = 15;
        $b1 = 20;
        
        echo addone($a1, $b1);
        echo "<br/>";
        
        function addone($n1, $n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        };
        
        echo addonetwo($a1, $b1);
        
        function addonetwo($n1, $n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        }
        ?>

The result output is:

17 22

17 22

 

If I change the code to add "&" before the "addone" function:

function addone(&$n1, &$n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        };

Then the output is:

17 22

19 24

 

I don't understand what's going on.  Why is the "&" incrementing the changed variable and in the first example it's incrementing the variables as defined.

think about it this way...

 

when you don't pass by reference:

 

$a1 ----> [15]
$n1 ----> [15]

 

if you change the [15] for $n1, it just changes that because nothing else is pointing to that [15]

 

when you pass by reference it's like this:

 

$a1 -----> [15]
$n1 ---------^

so when you change the [15] for $n1, it also changes for $a1

 

in your example using references (added comments):

 <?php

        $a1 = 15;
        $b1 = 20;
        
        //outputs the 15+2 and the 20+2, also now $a1 and $b1 become 17 and 22
        echo addone($a1, $b1); 
        echo "<br/>";
        
        function addone(&$n1, &$n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        };

        //outputs the 17+2 and the 22+2, $a1 and $b1 remain unchanged at 17 and 22
        echo addonetwo($a1, $b1); 
        
        function addonetwo($n1, $n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        }
        ?>

Ok, I think I'm following you.

 

Adding the "&" reference is changing the $a1 and $b1 variables.  This made a lot more sense when I put the reference marker on the "addonetwo" function and then added a third statement that incremented the returned output.

 

$a1 = 15;
        $b1 = 20;
        
        echo addone($a1, $b1);
        echo "<br/>";
        
        function addone($n1, $n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        };
        
        echo addonetwo($a1, $b1);
        echo "<br/>";
        
        function addonetwo(&$n1, &$n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        }
        
        function addonetwothree($n1, $n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        }
        echo addonetwothree($a1, $b1);
        ?>

 

Then the output becomes:

17 22

17 22

19 24

 

You all are great! Thank you for the help.

 

One related question.  Let's say this was a longer more complicated function with other increments, how would I call back to the original $variables even after they had been incremented and changed.  Let's say I wanted to add a "addonetwothreefour" function:

 

function addonetwothreefour($n1, $n2){
            $n1 = $n1 += 2;
            $n2 = $n2 += 2;
            return $n1 . " " . $n2;
        }

 

and have it return:

17 22

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.