RE: [PHP] Accessing a variable from inside a local function

From: Date: Wed, 28 Jul 2004 12:39:19 +0000
Subject: RE: [PHP] Accessing a variable from inside a local function
Groups: php.general 
Request: Send a blank email to php-general+get-192090@lists.php.net to get a copy of this message
On 28 July 2004 12:29, Frank Munch wrote: > Would anyone know how to resolve this scope problem? Or is > this possibly > something PHP can't (yet) do? > > The problem is to access a variable in a function foo from a function > local to foo. Actually, your problem is that the function bar is *not* local to foo. Even though PHP lets you declare functions lexically within the scope of other functions, semantically they are still global functions and have no access whatsoever to the enclosing function. > > - - - > > function foo($var_foo) > { > $my_foo_local = 10; //How can I reach this from inside function > bar(...? > > function bar($var_bar) > { > return $my_foo_local+$var_bar+1; // Doesn't work, can't see > $my_foo_local } > > return bar($var_foo)+1; > } > > if (foo(0)==12) > echo "Yes! I did it!"; > else > echo "Nope. Sorry"; Cheers! Mike --------------------------------------------------------------------- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: m.ford@leedsmet.ac.uk Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#192090) next »