Newbie question...

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

Post Reply
fluidbyte
Forum Commoner
Posts: 30
Joined: Tue May 27, 2008 2:07 pm

Newbie question...

Post 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)
}
 
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Newbie question...

Post 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?
fluidbyte
Forum Commoner
Posts: 30
Joined: Tue May 27, 2008 2:07 pm

Re: Newbie question...

Post 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?
Post Reply