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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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;
        }
        ?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Never mind, after sleeping on it, I think I can answer my own question.  There is no way to call the original values after the have been passed by reference because the $variable has been assigned a new value and only exists in that form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.