Page 1 of 1
include() vars within functions!
Posted: Sun Oct 09, 2005 3:29 am
by xtjdx
Here is my dilemma!
I am including a file full of config vars.
My code is basically:
Code: Select all
<?
include_once('config.inc.php');
echo $randomvar;
function showvar()
{
echo $randomvar;
}
showvar();
?>
This will output the value of $randomvar once for the regular echo, but not in the function.
The content of config.inc.php is:
What's going on here?
Am I totally inept?
Thank you in advance for any help offered!
Re: include() vars within functions!
Posted: Sun Oct 09, 2005 3:50 am
by blacksnday
xtjdx wrote:Here is my dilemma!
I am including a file full of config vars.
My code is basically:
Code: Select all
<?
include_once('config.inc.php');
echo $randomvar;
function showvar()
{
echo $randomvar;
}
showvar();
?>
This will output the value of $randomvar once for the regular echo, but not in the function.
The content of config.inc.php is:
What's going on here?
Am I totally inept?
Thank you in advance for any help offered!
Code: Select all
<? include_once('config.inc.php');
function showvar($randomvar)
{
echo $randomvar;
}
showvar($randomvar);
?>
or
Code: Select all
<? include_once('config.inc.php');
function showvar()
{
global $randomvar;
echo $randomvar;
}
showvar()
?>

Posted: Sun Oct 09, 2005 11:21 am
by xtjdx
Bahhhh that is going to be a whole lot of extra code. Thank you.
Posted: Sun Oct 09, 2005 11:41 am
by pilau
So what you mean is that functions can't call variables which were declared outside of them?
Posted: Sun Oct 09, 2005 11:45 am
by feyd
pilau wrote:So what you mean is that functions can't call variables which were declared outside of them?
bingo. It's called variable scope.
http://php.net/language.variables.scope
Posted: Sun Oct 09, 2005 5:38 pm
by xtjdx
Very interesting. I saw something about that and said "hey, that's probably my problem!" but fell asleep halfway into reading it.