Search code examples
phppage-title

Page.Title equivalent in PHP


I want to change the title of my PHP pages dynamically but since the headers and footer sections are include files so I cannot use <title><?php echo $title;?></title> kind of solution.

I am wondering if there is any solution such as the one in Asp.NET Page.Title = "Some Title"; maybe like $page->set_title('Programmers Palace');

Many thanks...


Solution

  • Since the headers and footer sections are include files so I cannot use <title><?php echo $title;?></title> kind of solution.

    You can. Just because the files are included doesn't mean you can't echo things there.

    If you had a Page object...

    index.php

    <?php
    
    $page = new Page('Programmers\' Palace');
    
    include 'includes/header.php';
    

    includes/header.php

    <head>
      <title><?php echo $page->getTitle(); ?></title>
    </head>