Re: Re: Include and require

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Adam Zey wrote:
Dave Goodchild wrote:
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', 7 => '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@xxxxxx <mailto:azey@xxxxxx>> wrote:

    Dave Goodchild wrote:
    > Hi all - I have several require_once statements in my web app to
    load in
    > small function libraries. A common one bundles a variety of
    functions to
    > handle date math and map month numbers to month names. I
    originally defined
    > an array in that file plus a bunch of functions but when I
    loaded the page,
    > the array variable, referenced further down the page, was NULL.
    I wrapped a
    > 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 the
    array variable
    > is referenced in the web page, not in any function.
    >

    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
Was 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.

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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux