Dumb Variable Scope question (variable within {})

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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Dumb Variable Scope question (variable within {})

Post by JAB Creations »

I can't access a variable defined within {} curely brackets, I know it's a scope issue though I didn't see anything on the variable.scope page on php.net. So a quick reminder on what I should do to access $theme variable below would be most helpful! I'm just setting some default values in the absolute certain situation that new visitors who have never been to my site have some preferences set (versus none).

Code: Select all

if (!isset($_COOKIE['settings']))
{
$theme = 'classic';
}
 
//Below does not echo anything, how can we change that?
echo $theme;
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Dumb Variable Scope question (variable within {})

Post by Ambush Commander »

PHP doesn't have block level scopes, only function scopes. It sounds like the above code segment is never being run: stick an echo and check it out.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Dumb Variable Scope question (variable within {})

Post by JAB Creations »

Actually I ended up moving the variables out of the function and everything works fine now.
LSJason
Forum Commoner
Posts: 45
Joined: Mon May 12, 2008 4:43 pm

Re: Dumb Variable Scope question (variable within {})

Post by LSJason »

For future reference, you can just do this:

Code: Select all

 
global $theme; // Set the variable as global to the current scope
if (!isset($_COOKIE['settings']))
{
$theme = 'classic';
}
//Below does not echo anything, how can we change that?
echo $theme;
 
Post Reply