Re: unexpected modification of a variable

From: Date: Wed, 29 Jun 2011 15:00:42 +0000
Subject: Re: unexpected modification of a variable
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-53662@lists.php.net to get a copy of this message
On 29 June 2011 15:53, Pascal COURTOIS <Pascal.Courtois@nouvo.com> 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 ? 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

« previous php.internals (#53662) next »