For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > June 2005 > include() and scopes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author include() and scopes
Mitja

2005-06-01, 3:55 am

Hello,

given the following scenario, how can I make _a visible in 3.php? Below is
what I /tought/ would work...

1.php:
global $_a;
$_a = 42;

2.php:
include('1.php');
include('3.php');
foo();

3.php:
function foo() {
print $_a; //prints nothing
}


TIA,
Mitja
Oli Filth

2005-06-01, 3:56 pm

Mitja said the following on 01/06/2005 00:45:
> Hello,
>
> given the following scenario, how can I make _a visible in 3.php? Below
> is what I /tought/ would work...
>
> 1.php:
> global $_a;
> $_a = 42;
>
> 2.php:
> include('1.php');
> include('3.php');
> foo();
>
> 3.php:
> function foo() {
> print $_a; //prints nothing
> }
>


Declaring a variable as global outside of any function (as you've done)
does nothing.

Move the "global $_a" declaration into the foo() function.


--
Oli
Mitja

2005-06-01, 3:56 pm

On Wed, 01 Jun 2005 15:25:27 +0200, Oli Filth <catch@olifilth.co.uk> wrote:

> Mitja said the following on 01/06/2005 00:45:
>
> Declaring a variable as global outside of any function (as you've done)
> does nothing.
>
> Move the "global $_a" declaration into the foo() function.
>


Fancy seeing you here... :)
Thanks a bunch, works just the way it should.

Mitja
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com