'Dynamic Title Tag in PHP

I am trying to dynamically populate the title tag on a website. I have the following code in my index.php page

<?php $title = 'myTitle'; include("header.php"); ?>

And the following on my header page

<title><?php if (isset($title)) {echo $title;}
else {echo "My Website";} ?></title>

But no matter what I do, I cannot get this code to work. Does anyone have any suggestions?

thanks



Solution 1:[1]

Are you including the header file before or after you set the title variable? If you're including it before, then of course it won't be set.

if you're doing something like this in your index.php:

<?php
     include('header.php');
     $title = "blah blah blah";
?>

then it won't work - you include the header file and output the title text before the $title variable is ever set.

Solution 2:[2]

try to declare the variable before using it

$title = '123';
require 'includes/header.php';

Solution 3:[3]

Hi Try this old school method ..

In your Header file (for e.g. header.php)

<?php 
error_reporting(E_ALL);

echo '<!DOCTYPE html>
<!--[if IE 7 ]><html class="ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie8" lang="en"><![endif]-->
<!--[if IE 9 ]><html class="ie9" lang="en"><![endif]-->
<!--[if (gte IE 10)|!(IE)]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<!--<![endif]-->
<head>'; 
?>
<?php  
if($GLOBALS['title']) {
    $title = $GLOBALS['title'];
} else {
    $GLOBALS['title'] = "Welcome to My Website";        
}
if($GLOBALS['desc']) {  
    $desc = $GLOBALS['desc'];
} else {
    $desc = "This is a default description of my website";      
}
if($GLOBALS['keywords']) {
    $keywords = $GLOBALS['keywords'];   
} else {
    $keywords = "my, site, key, words";     
}
echo "\r\n";
echo "<title> ". $title ." | MyWebsite.com </title>";
echo "\r\n";
echo "<meta name=\"description\" content='". $GLOBALS['title']."'>";
echo "\r\n";
echo "<meta name=\"keywords\" content='".$GLOBALS['title']."'>";
echo "\r\n";
?>

In you PHP Page file do like this (for example about.php)

<?php

$GLOBALS['title'] = 'About MyWebsite -This is a Full SEO Title';
$GLOBALS['desc'] = 'This is a description';
$GLOBALS['keywords'] ='keyword, keywords, keys';

include("header.php");


?>

Solution 4:[4]

I assume your header is stored in a different file (could be outside the root directory) then all the above solutions will not work for you because $title is set before it is defined.

Here is my solution: in your header.php file you need to set the $title to be global by: global $title; then echo it in your title so:

<?php global $title; ?>
<title><?php echo isset($title) ? $title : "{YOUR SITE NAME}"; ?></title>

Then in every page now you can define your title after you have included your header file so for example in your index.php file:

include_once("header.php");
$title = "Your title for better SEO"

This is tested and it is working.

Solution 5:[5]

We can also use functions and its a good way to work on real time web sites.

Do simple:

create an index.php file and paste these lines:

<?php include("title.php");?>
<!doctype html>
<html>
<head>
    <title><?php index_Title(); ?></title>
<head>
</html>

-- Then

Create a title.php file and paste these lines:

<?php
function index_Title(){
$title = '.:: itsmeShubham ::.';
if (isset($title)){
    echo $title;
    }else{
        echo "My Website";
    };
}
?>

It will work perfectly as you want and we can also update any title by touching only one title.php file.

Solution 6:[6]

<?php
    echo basename(pathinfo($_SERVER['PHP_SELF'])['basename'],".php");
?>

This works. Since I'm using PHP I don't check for other extensions; use pathinfo['extension'] in case that's required.

Solution 7:[7]

You can achieve that by using define(); function.
In your header.php file add following line :

<title><?php echo TITLE; ?></title>

And on that page where you want to set dynamic title, Add following lines:
EX : my page name is user-profile.php where I want to set dynamic title so I will add those lines that page.

<?php
    define('TITLE','User Profile'); //variable which is used in header.php
    include('header.php');
    include('dbConnection.php');
?>

So my user-profile/.php file will be having title: User Profile
As like this you can add title on any page on your site

Solution 8:[8]

Example Template.php

<?php
if (!isset($rel)) {$rel = './';}
if (!isset($header)) {
  $header = true;
?><html>
  <head>
    <title><?php echo $pageTitle; ?></title>
  </head>
<body>

<?php } else { ?>

</body>
</html><?php } ?>

Solution 9:[9]

Pages Your Content

<?php
  $rel = './'; // location of page relative to template.php
  $pageTitle = 'This is my page title!';
  include $rel . 'template.php';
?>

Page content here

<?php include $rel . 'template.php'; ?>

Solution 10:[10]

I'm using your code in my project and it works properly

My code in header:

 <title>
  <?php 
   if (isset($title)) {echo $title;}
   else {echo "?????? ???? ???!";} 
  ?>
 </title>

and my code in index.php:

<?php
  $title = "???? ???? ????";
  include("./include/header-menu.php");
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marc B
Solution 2 Bassem Shahin
Solution 3 Sashi
Solution 4 Amir Hassan Azimi
Solution 5
Solution 6 user4157124
Solution 7
Solution 8 Codeside ID
Solution 9 Codeside ID
Solution 10 M.Eshagh