PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
xtjdx
Forum Newbie
Posts: 5 Joined: Sun Oct 09, 2005 3:23 am
Post
by xtjdx » Sun Oct 09, 2005 3:29 am
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!
blacksnday
Forum Contributor
Posts: 252 Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(
Post
by blacksnday » Sun Oct 09, 2005 3:50 am
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()
?>
xtjdx
Forum Newbie
Posts: 5 Joined: Sun Oct 09, 2005 3:23 am
Post
by xtjdx » Sun Oct 09, 2005 11:21 am
Bahhhh that is going to be a whole lot of extra code. Thank you.
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Sun Oct 09, 2005 11:41 am
So what you mean is that functions can't call variables which were declared outside of them?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Oct 09, 2005 11:45 am
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
xtjdx
Forum Newbie
Posts: 5 Joined: Sun Oct 09, 2005 3:23 am
Post
by xtjdx » Sun Oct 09, 2005 5:38 pm
Very interesting. I saw something about that and said "hey, that's probably my problem!" but fell asleep halfway into reading it.