include() and scopes

This is a discussion on include() and scopes within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello, given the following scenario, how can I make _a visible in 3.php? Below is what I /tought/ would ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-01-2005
Mitja
 
Posts: n/a
Default include() and scopes

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
Reply With Quote
  #2 (permalink)  
Old 06-01-2005
Oli Filth
 
Posts: n/a
Default Re: include() and scopes

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
Reply With Quote
  #3 (permalink)  
Old 06-01-2005
Mitja
 
Posts: n/a
Default Re: include() and scopes

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


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

Mitja
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:21 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0