Lodius2000 Posted April 23, 2009 Share Posted April 23, 2009 Hi all long time no see I tend to keep my layouts in separate files from my main php code and call the layout from within php, most of the time the layout is called within a function also... so heres my problem i have a layout page that contains the code... headTemplate.php <html> <head> <title><?php print $pagetitle; ?></title> ...it goes on from there now head template gets called from index.php like so <?php function show_form($errors = '') { $pagetitle= "BLAH BLAH BLAH"; require_once('templates/headTemplate.php'); } show_form(); ?> but the BLAH BLAH BLAH is not displayed in the title bar of my window, am I somehow throwing this variable out of scope? what could be wrong? thanks Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/ Share on other sites More sharing options...
mikesta707 Posted April 23, 2009 Share Posted April 23, 2009 variables in user defined functions have a local scope, so the variable you are using to set the title isnt usable by that page. An alternative would be to do make the variables in your function global, IE: <?php function show_form($errors = '') { global $pagetitle= "BLAH BLAH BLAH"; require_once('templates/headTemplate.php'); } show_form(); ?> I believe that would make it so that you could use that variable in your other page. Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817541 Share on other sites More sharing options...
tang Posted April 23, 2009 Share Posted April 23, 2009 $pagetitle should be being passed to 'templates/headTemplate.php' if that's what you mean? It won't be available anywhere else in your code though. Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817547 Share on other sites More sharing options...
Lodius2000 Posted April 23, 2009 Author Share Posted April 23, 2009 tang, yes $pagetitle should be passed to headTemplate but it is not. mikesta, good suggestion but it didnt work, you cant declare a global variable and define it in the same line so I broke it apart and that didnjt work, then i defined the var outside the function, then made it global. then I put the global declaration inside of headTemplate at the top and that didnt work either any other suggestions from the crowd, Im not new at this php thing but this is making me nuts. Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817556 Share on other sites More sharing options...
tang Posted April 23, 2009 Share Posted April 23, 2009 Your original code should definitely work, can you show us headTemplate.php please? Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817558 Share on other sites More sharing options...
Lodius2000 Posted April 23, 2009 Author Share Posted April 23, 2009 i already did, that is what is there, html, head and title Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817561 Share on other sites More sharing options...
redarrow Posted April 23, 2009 Share Posted April 23, 2009 Has the added template got the variable. $pagetitle= "BLAH BLAH BLAH"; <<<<<<<<<<<<< require_once('templates/headTemplate.php'); in this page? make sure the directory/to the file is correct! Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817563 Share on other sites More sharing options...
Lodius2000 Posted April 23, 2009 Author Share Posted April 23, 2009 AAAAAAAAAAAAAAAAAAAAAAAAARHG $pagetitle != $pageTitle Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817568 Share on other sites More sharing options...
tang Posted April 23, 2009 Share Posted April 23, 2009 Just tested your original code and it works fine for me. Is that the very start of your headTemplate.php or is there anything preceeding it? EDIT: W00t ;-) Quote Link to comment https://forums.phpfreaks.com/topic/155382-solved-calling-an-include-from-within-a-function-variable-scope-question/#findComment-817571 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.