Page 1 of 1

Variable scope

Posted: Tue Jan 20, 2004 8:08 pm
by Stryks
Hi all.

Please excuse this possibly totally basic question, but I cant seem to find any info on the problem I'm having.

I've got one file (index.php) which includes two files. The first file holds some info regarding the loacation of files and so on, so that I can make changes later. They are set in that include file with :

Code: Select all

$STDB_USER = 'member_auth';
That is fine, and I can use it in index.php, but I was hoping I could also use it in the second included file.

I guess I sort of had this image of the included file being parsed as though it was a part of the file it was included from, hence putting it in the same scope.

Am I making sense? Is there any way of doing what I'm looking for?

Thanks

Posted: Tue Jan 20, 2004 8:14 pm
by markl999
There's no problem with doing :
//file1.php
<?php
include_once 'inc1.php';
include_once 'inc2.php';
?>
//inc1.php
<?php
$test = 'hello';
?>
//inc2.php
<?php
echo $test;
?>

if that's what you mean?

Posted: Tue Jan 20, 2004 9:10 pm
by Stryks
Yeah, thats it exactly.

But it doesnt seem to work. If I echo the value then it is just blank.

Any ideas?

Posted: Tue Jan 20, 2004 9:17 pm
by Stryks
Ok, on second thoughts, thats not exactly it.

The second include file has a function which uses the variable established in include 1.

The function is then called from index.php

//file1.php
<?php
include_once 'inc1.php';
include_once 'inc2.php';

example();
?>

//inc1.php
<?php
$test = 'hello';
?>

//inc2.php
<?php
function example()
{
echo $test;
}
?>

Posted: Tue Jan 20, 2004 9:40 pm
by Stryks
Ahhh :)

//inc2.php
<?php
function example()
{
global $test
echo $test;
}
?>

That works

Posted: Wed Jan 21, 2004 11:12 am
by dull1554

Posted: Wed Jan 21, 2004 11:20 am
by Roja
That has got to be the most completely useful and hilarious image I have seen on the net in my 10+ years here.

Thank you so much for that.. I will make great use of it on my forums.

Posted: Wed Jan 21, 2004 2:45 pm
by dull1554
i thought so