RE: [PHP] Calling functions by reference

From: Date: Thu, 01 Jan 1970 00:00:00 +0000
Subject: RE: [PHP] Calling functions by reference
Groups: php.general 
Request: Send a blank email to php-general+get-18441@lists.php.net to get a copy of this message
Sorry, I was trying to shorten things, but clearly didn't explain it very well. Here is a new code fragment: function test_a(&$x, &$y) { if isset($y) return $y; else return $x; } $fred='Hello'; $dave=' Goodbye'; echo test_a($fred, $dave); The above stuff works as you would expect. function test_b(&$x, &$y = 0) { if (isset($y) && $y) return $y; else return $x; } $fred='Hello'; echo test_b($fred); This doesn't work, complaining about the missing ')' in the test_b definition. What I want to do is to be able to have *optional* parameters that are passed by reference. Ideally, these should arrive as "!isset($y)" if they are not passed in the call. Thanks, Chris. -----Original Message----- From: Wico de Leeuw [mailto:wico@cnh.nl] Sent: 03 October 2000 17:07 To: cjsmith@btinternet.com; php-general@lists.php.net Subject: Re: [PHP] Calling functions by reference At 15:45 3-10-00 +0000, cjsmith@btinternet.com wrote: >Hi, > >I am trying to write a function that has parameters >passed by reference, and was wondering how to get a >default value to them. > >e.g.: >function test(&$a, &$b = 0) $a = "plop"; $b = 1; test(&$a, &$b); ... function test($a, $b = 0) { } >doesn't work (PHP complains about expecting a ')' after >the second parameter). Is there some other >syntactically-valid way to achieve this, or do I have to >provide *all* variables when I call this function? (This is >a pain, as you can't statically assign values at the other >end either - you have to define a temporary variable >with no content). > > >Thanks in advance, > >Chris.

« previous php.general (#18441) next »