toolman Posted October 25, 2009 Share Posted October 25, 2009 Hi, I am having a problem managing my page title with PHP. Currently I have my <title> tags in each PHP document, followed by my header.php which has my functions.php file contained in it. The functions.php has my page titles defined in it. I can only get my titles to show if I place my header.php file above the <title> tag. Is there a way round this problem? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/178985-php-title-problem/ Share on other sites More sharing options...
Mark Baker Posted October 25, 2009 Share Posted October 25, 2009 Very simple answer. No. If you're only defining the values for your title when you include your header.php, then you can't echo them out before they're defined You could include functions.php at the top of your pages before echoing the title, rather than including it through header.php Quote Link to comment https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944304 Share on other sites More sharing options...
dreamwest Posted October 25, 2009 Share Posted October 25, 2009 If title is a variable and your calling it before you defined it, of course it wont display anything <?php echo "<title>{$title_name}</title>"; $title_name = "Title here"; ?> You will need functions.php above the title tag <?php include('functions.php'); //which has the $title_name variable in it echo "<title>{$title_name}</title>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944306 Share on other sites More sharing options...
toolman Posted October 25, 2009 Author Share Posted October 25, 2009 Thanks for the replies. Is it good practice to include a file right before the doctype on each php page? Also, at the moment, I have set up mt title tag to use this: <title><?php echo "$site_title"; echo"$website_settings_title" ;?></title> Is there a way to not use 2 echos? Quote Link to comment https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944312 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2009 Share Posted October 25, 2009 Is it good practice to include a file right before the doctype on each php page? It does not matter. A doctype only matters when it has been sent to the browser. When a doctype is in a source file on the server is it just a bunch of inert characters. Php outputs content - HTML/CSS/Javascirpt/media... to a browser. The only thing that matters is that the resulting HTML/CSS/Javascirpt/media is valid and accomplishes what you desire for that page. As long as an include statement comes before where the code in the included file is needed it does not matter. A general rule would be that any initialization that php needs on the page, such as including files, stating sessions, determining if the visitor is logged in and/or has permission to access the page... should come first. You then build the content that makes up the page and output it. Quote Link to comment https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944320 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.