# unexpected modification of a variable

- Thread: https://externals.io/message/53658
- Original archive: https://news-web.php.net/php.internals/53658
- Started by Pascal COURTOIS on 2011-06-29T14:53:47+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #53658 — Pascal COURTOIS — 2011-06-29T14:53:47+00:00

URL: https://externals.io/message/53658#53658

Hi,

Is there any way that a variable can be changed within a function without passing it by reference ?

I have a code like that:

function myfunction($var)
{
  
  <some code> 
  print_r($var); => prints $var which is an object
  anotherfunction($var);  // call by value
  print_r($var); => $var has changed

}


I thought that since there's no reference in any way to the variable it cannot be changed in the function. Am I wrong ?

---

## #53662 — Richard Quadling — 2011-06-29T15:00:42+00:00

URL: https://externals.io/message/53658#53662 · In reply to: #53658

> Hi,
>
> Is there any way that a variable can be changed within a function without passing it by reference ?
>
> I have a code like that:
>
> function myfunction($var)
> {
>
>  <some code>
>  print_r($var); => prints $var which is an object
>  anotherfunction($var);  // call by value
>  print_r($var); => $var has changed
>
> }
>
>
> I thought that since there's no reference in any way to the variable it cannot be changed in the function. Am I wrong ?

For variables ...

<?php
$variable = 'rock';

function paper() {
	global $variable;
	$variable = __FUNCTION__;
}

function sissors() {
	$GLOBALS['variable'] = __FUNCTION__;
}

echo $variable, PHP_EOL;
paper();
echo $variable, PHP_EOL;
sissors();
echo $variable, PHP_EOL;
?>

For objects, $var is always an alias to an object identifier which
points to the same object...

http://docs.php.net/manual/en/language.oop5.references.php



-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

---

## #53667 — Pascal COURTOIS — 2011-06-29T16:09:00+00:00

URL: https://externals.io/message/53658#53667 · In reply to: #53662

Le 29/06/2011 17:00, Richard Quadling a écrit :

> For objects, $var is always an alias to an object identifier which
> points to the same object...
> 
> http://docs.php.net/manual/en/language.oop5.references.php

 thanks

---

## #53688 — keisial@gmail.com — 2011-06-29T22:25:06+00:00

URL: https://externals.io/message/53658#53688 · In reply to: #53658

Pascal COURTOIS wrote:
> Hi,
>
> Is there any way that a variable can be changed within a function without passing it by reference ?
>
> I have a code like that:
>
> function myfunction($var)
> {
>
>    <some code>
>    print_r($var); =>  prints $var which is an object
>    anotherfunction($var);  // call by value
>    print_r($var); =>  $var has changed
>
> }
>
>
> I thought that since there's no reference in any way to the variable it cannot be changed in the function. Am I wrong ?
Maybe the prototype of anotherfunction() receives it by reference.
Another option would be that <some code> sets some global as a reference 
to $var, and $var is changed through it.

If you have such problem, you should provide a test case. But it's 
probably a bug in your code that will get solved when piecing it.
