![]() |
PHP references question
I suspect I'm missing something very obvious here. Can someone explain the behaviour of this code:
Code:
<?php Inside test:12345 anothertest:12345 yetanothertest: Outside test:12345 anothertest: yetanothertest:12345 In other words, within the function, the reference assignment seems to have updated $anothertest but not $yetanothertest, yet having left the function, it's the other way round. With direct assignments ($A = $B) instead of references ($A =& $B), both of the global variables are updated and correctly displayed inside and outside the function. |
What values were you expecting?
PHP Code:
and PHP Code:
I know I'm not explaining it that well, there's a section in the PHP manual dedicated to explaining references over at php.net. |
Quote:
As you say, it's explained in the php help manual: Warning If you assign a reference to a variable declared global inside a function, the reference will be visible only inside the function. You can avoid this by using the $GLOBALS array. Think about global $var; as a shortcut to $var =& $GLOBALS['var'];. Thus assigning other reference to $var only changes the local variable's reference. I didn't get that far on that page. |
All times are GMT. The time now is 06:51 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|