Re: Scope of include

From: Date: Thu, 18 Jan 2007 15:39:29 +0000
Subject: Re: Scope of include
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-247339@lists.php.net to get a copy of this message
On Thu, 2007-01-18 at 10:29 -0500, tedd wrote: > At 9:49 PM -0800 1/17/07, jekillen wrote: > >Hello php list: > >If I include a php script inside a php function definition and then call the > >function in another script. What is the scope of variables in the included > >script? Are they local to the function that calls include with the file name? > >Thanks in advance; > >I'm not sure where to look for this answer; > >JK > > > JK: > > Whenever I have a question about an include, I just cut/paste (i.e., > replace) the include statement with the include script. That way I > know exactly what it does. Sounds like a maintenance nightmare :| To answer his question though, variables not declared via the "global" keyword and not accessed via the super global variables ($GLOBALS, $_SESSION, etc) inherit the scope in which the include statement occurs. Therefore, if in the included file all you have is: $foo = 'foo'; Then it's scope is the function including the file. Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#247339) next »