2

I'm running into an issue with changing a global variable to another reference inside a function.

$one = 1;
$two = 2;
$ref = &$one;

change();
echo $ref;

function change(){
    global $ref, $two;
    $ref = &$two;
}

The result of the code is "1". I don't really understand why, I would have guessed changing the global variable inside the function it would be persistent after leaving the function scope?

What would be a good workaround (apart from using the GLOBALS array)?

1
  • Try $ref = $two; Commented Oct 14, 2018 at 14:50

2 Answers 2

2

Actually that was a great question. In order to have references returned from functions, use the & before the function name as below.

$one = 1;
$two = 2;
$ref = &$one;

$ref = &change();
echo $ref;
$two = 3;
echo $ref;

function &change(){
    global $two;
    return $two;
}

Output:

23
Sign up to request clarification or add additional context in comments.

1 Comment

In the end we went for a different solution and used a file-include instead of a function as a workaround. Not a very neat solution, but in the case we had it was better than to use a return value (reference) and change the variable outside the function.
2

In PHP, reference is NOT pointer. It is something like alias of another variable. I will explain what happen with your code:

$one = 1;
$two = 2;
$ref = &$one;

After three commands above, we have:

variables   | $one | $ref | $two |
content     |    1        | 2    |

As you see, $one and $ref refer to the same content, it is what the term reference meaning. Continue:

global $ref, $two;

According to this document, above command the same as:

$ref =& $GLOBALS['ref'];
$two =& $GLOBALS['two'];

So, we have:

variables   | $one (global) | $ref(global) | $ref (local) | $two (global) | $two (local) |
content     |                    1                        |               2              |

Yes, we have 5 variables! Continue:

$ref = &$two;

It is actually:

$ref (local) = &$two (local);

So we have:

variables   | $one (global) | $ref(global) | $ref (local) | $two (global) | $two (local) |
content     |                    1         |                        2                    |

And, the last command:

echo $ref;

Actually is:

echo $ref (global);

And, 1 is the correct value!

Additional:

change();
echo $two;

function change(){
    global $ref, $two;
    $ref = &$two;
    $ref = 9;
}

The result of this code is 9;

----- EDIT -----

I did not read the question carefully. My answer is for the part The result of the code is "1". I don't really understand why. The answer of Jonathan Gagne is what you are looking for.

3 Comments

You did not make it bro. The echo output 9 only because you assign it inside the function. He just has to use & before the function name. If he changes the value of his variable $two it is not referencing to the $ref variable. Look my solution please you will understand what he was asking for.
I edited my answer. I did not read the question carefully. But, is there any wrong with my answer?
Related to his question, If you put $one = 1; $two = 2; $ref = &$one; echo $ref; change(); echo $ref; $two = 3; echo $ref; it will output 199 instead 193 as he wants. However, your answer was really instructive and descriptive of the PHP global variable. But did not represent a solution for this question. Thanks for all btw!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.