Guest kilbad Posted October 17, 2006 Share Posted October 17, 2006 Most pages on my website are generated with three [i]include[/i] commands, such that the header and footer are the same for all::[code]include 'header.php';include "$some_unique_page.php";include 'footer.php';[/code]In my header, I want to have the following code::[code]<title><?php echo "$page_title_of_unique_content"; ?></title>[/code]..and I want the $page_title_of_unique_content variable to be defined in the $some_unique_page.php file.[b]Question:: Is it possible somehow echo this variable in the header title when it is being defined later on in the included script?[/b] Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/ Share on other sites More sharing options...
rab Posted October 17, 2006 Share Posted October 17, 2006 No, not if it is already defined Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/#findComment-110322 Share on other sites More sharing options...
Guest kilbad Posted October 17, 2006 Share Posted October 17, 2006 It is not defined already, just later, in the some_unique_page.php Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/#findComment-110336 Share on other sites More sharing options...
obsidian Posted October 17, 2006 Share Posted October 17, 2006 trying to echo a variable before it is defined would be like me expecting you to be able to solve a math problem for me without ever seeing the equation. there is no way PHP knows what to output if a variable has not been defined. why, exactly, can't the title variable be set somewhere else before the header is output? Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/#findComment-110340 Share on other sites More sharing options...
btherl Posted October 18, 2006 Share Posted October 18, 2006 Smarty templates will solve this problem. It's a big step from standard php to Smarty though.Another option is to buffer your output using ob_start() and related functions. Then you can modify the buffer before displaying it, with str_replace() for example.[code]<? ob_start() ?><title>{PAGE_TITLE}</title>...$output = ob_get_clean();$output = str_replace('{PAGE_TITLE}', $page_title_of_unique_content, $output);print $output;[/code] Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/#findComment-110450 Share on other sites More sharing options...
Guest kilbad Posted October 18, 2006 Share Posted October 18, 2006 This is exactly what I needed!! Thank you so much! Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/#findComment-110714 Share on other sites More sharing options...
Guest kilbad Posted October 18, 2006 Share Posted October 18, 2006 Follow-up question (I am new to smarty templates)..[b]With regard to smarty tags (like {PAGE_TITLE} for example, if I leave it undefined within some php document, how can I make it so that nothing is displayed, rather than "{PAGE_TITLE}"?[/b]Restated, in the above example, if {PAGE_TITLE} was undefined, I would want no title displayed, NOT "{PAGE_TITLE}"..Thanks in advance for the help!!Brendan Link to comment https://forums.phpfreaks.com/topic/24275-echo-variable-before-it-is-defined-follow-up-question-nevermind/#findComment-110803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.