Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/178985-php-title-problem/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944304
Share on other sites

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>";

?>

Link to comment
https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944306
Share on other sites

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944312
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/178985-php-title-problem/#findComment-944320
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.