Page 1 of 1

Newbie question...

Posted: Tue May 27, 2008 2:10 pm
by fluidbyte
I'm really new to php (though I've been doing asp/.net for awhile now).

My question is how to I create a variable that I can use in a function without passing it to the function?

Code: Select all

 
 
$myVariable = "Something"
 
function myFunction()
{
  echo($myVariable)
}
 
 

Re: Newbie question...

Posted: Tue May 27, 2008 2:13 pm
by onion2k
You can fetch it from the main branch of the script by using "global $myVariable;" before you use it, but that really is a bad idea. Globals are horrid. Why can't you pass it in?

Re: Newbie question...

Posted: Tue May 27, 2008 2:19 pm
by fluidbyte
It's going to be a switch statement to pull in a variable (set from a configuration file). I just don't want to pass in all of the possible variables.

Is there a better way besides a global or passing it in?

Would it make sense to build an array with all the settings in it and then pass it into any function where I need it?