OM2 Posted September 15, 2011 Share Posted September 15, 2011 I need a bit of help understanding the scope of variables If I have the following code: <?php $myvar = true; include(otherfile.php); ?> Is $myvar available for access as is in the code from otherfile.php? Specifically though, I'm trying to get my head round some Wordpress coding, so I have this instead: <?php $myvar = true; <?php get_header(); ?> ?> <?php get_header(); ?> puts the contents of header.php at this location + I'm sure it does some other things like error checking etc What is the best way to have $myvar available? The ONLY thing is I only want $myvar = true just for one file - everywhere else it should be false or not exist I'm wary of using a global variable - but if it's the only way, then I have to Thanks Omar Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/ Share on other sites More sharing options...
dougjohnson Posted September 15, 2011 Share Posted September 15, 2011 $myvar would equal "true" in the page where it was given the value "true" AND in the included page otherfile.php. In all other pages, $myvar would equal "" unless it was set to some other value. I think that's what you want? You would probably get a php warning in the 'other' pages if $myvar was referenced and not initialized. Or, I may not understand your question. In which case I apologize. Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269651 Share on other sites More sharing options...
OM2 Posted September 15, 2011 Author Share Posted September 15, 2011 thanks for confirming the problem is with this version: <?php $myvar = true; <?php get_header(); ?> ?> $myvar doesn't seem available in header.php - get_header() puts the code of the file in the place, so i thought similar to the above the variable would be available i'm sure i'm doing something really simple wrong? Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269655 Share on other sites More sharing options...
ManiacDan Posted September 15, 2011 Share Posted September 15, 2011 Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include) In your second example, $myvar will not be available in get_header() because functions don't access the global scope. If you wish $myvar to be available in get_header you'll have to pass it in. (It should be noted that your second example is a PHP fatal parse error). The manual page for variable scope will tell you more. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269656 Share on other sites More sharing options...
OM2 Posted September 15, 2011 Author Share Posted September 15, 2011 Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include) In your second example, $myvar will not be available in get_header() because functions don't access the global scope. If you wish $myvar to be available in get_header you'll have to pass it in. (It should be noted that your second example is a PHP fatal parse error). The manual page for variable scope will tell you more. -Dan thanks dan, that makes more sense the fact that get_header() spits out the contents of header.php - does not mean it has access to variables in other files in that case, i only have the choice of using a global variable? Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269658 Share on other sites More sharing options...
KevinM1 Posted September 15, 2011 Share Posted September 15, 2011 Including a PHP file has the exact same effect as copying and pasting the code from the include file into the file you're working on. In your first example, $myvar is available in the global scope (not functions or classes) in otherfile.php (though it should be noted that you're not quoting your argument to include) In your second example, $myvar will not be available in get_header() because functions don't access the global scope. If you wish $myvar to be available in get_header you'll have to pass it in. (It should be noted that your second example is a PHP fatal parse error). The manual page for variable scope will tell you more. -Dan thanks dan, that makes more sense the fact that get_header() spits out the contents of header.php - does not mean it has access to variables in other files in that case, i only have the choice of using a global variable? Ideally, you'd simply modify the function to accept your $myvar variable as a parameter: function get_header($arg = null) { if (isset($arg)) { // do something with $arg } // continue as normal } // later in your code $myvar = "something"; get_header($myvar); Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269668 Share on other sites More sharing options...
OM2 Posted September 15, 2011 Author Share Posted September 15, 2011 yes... that makes sense except, get_header() is a wordpress function i was looking for the best optimal solution Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269670 Share on other sites More sharing options...
ManiacDan Posted September 15, 2011 Share Posted September 15, 2011 so you're saying there's a wordpress FUNCTION called get_header() that uses the CODE inside header.php? The wordpress function obviously include()s the header.php file inside itself. This is bad practice (because, shockingly, it makes scope very confusing) but wordpress is full of bad practices. If you really need to include a variable from one page into the header.php there must be a way to inject it into that function in some way. Can you pass it an array or something? Failing that, use this syntax inside header.php: global $myvariable; Note that it's wrong to do that, but you're working within the restrictions of a relatively terrible piece of software. Quote Link to comment https://forums.phpfreaks.com/topic/247212-need-help-understanding-the-scope-of-variables/#findComment-1269674 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.