Page 1 of 1

Dumb Variable Scope question (variable within {})

Posted: Sat May 24, 2008 2:17 pm
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;

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

Posted: Sat May 24, 2008 4:32 pm
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.

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

Posted: Sat May 24, 2008 4:48 pm
by JAB Creations
Actually I ended up moving the variables out of the function and everything works fine now.

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

Posted: Sat May 24, 2008 11:24 pm
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;