Re: Re: Include and require
| From: | Chris | Date: | Tue, 15 Aug 2006 00:20:02 +0000 |
| Subject: | Re: Re: Include and require | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-240671@lists.php.net to get a copy of this message | ||
Adam Zey wrote:
Dave Goodchild wrote:That's wrong sorry :) $ php -a Interactive mode enabled <?php $foo = "bar"; if ( $foo == "bar" ) { $baz = "narf"; } echo $baz; narf Works fine. If you only create the variable inside the "if" you won't be able to use it if the code doesn't get into the if (it'll be an undefined variable): $ php -a Interactive mode enabled <?php error_reporting(E_ALL); if (1 == 0) { $foo = "blah"; } echo $foo; Notice: Undefined variable: foo in - on line 6 -- Postgresql & php tutorials http://www.designmagick.com/I use a config file too. That was a sanity check. The file extract looked like this: $months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6=> 'June',Hi all - I have several require_once statements in my web app to7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December');which was called in with require_once. The reference to $months in the calling page, checked with var_dump, was NULL. When I wrapped it like this: function getmonths() {$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May', 6=> 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November', 12 => 'December'); return $months;} it worked. Not sure why the simple variable didn't work. On 14/08/06, *Adam Zey* <azey@nit.ca <mailto:azey@nit.ca>> wrote:Dave Goodchild wrote:small function libraries. A common one bundles a variety ofload inhandle date math and map month numbers to month names. Ifunctions toan array in that file plus a bunch of functions but when Ioriginally definedthe array variable, referenced further down the page, was NULL.loaded the page,function def around the array and returned it and all was fine. I may be suffering from mild hallucinations, but can you not define variables in a required file? It is not a scope issue as theI wrapped ais referenced in the web page, not in any function.array variableWas the $months variable created inside an if statement or something else? PHP's rules of scope say that variables created inside a code block (like an if, a for, a while, a foreach), they stop existing the moment you exit that code block. So this: $foo = "bar"; if ( $foo == "bar" ) { $baz = "narf"; } echo $baz; That code will output nothing, because $baz is empty by the time I try to output it.I know for a fact that you can define variables in PHP 4 and 5. The idea behind include and require is little more complex than copying and pasting the code. Many of my scripts include a config.php which has various variables created with setting information.Regards, Adam Zey.-- http://www.web-buddha.co.uk http://www.projectkarma.co.uk