The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End [Solved] Page Titles in PHP

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
  • #29529


    Let me first let you know I am new here but I figured it is time I try to improve my abilities with PHP and other more complex web design tools. I am 18 and do web design as a part-time job for one or two local businesses–nothing fancy. I read css-tricks a lot but this is my first time on the forum. My experience with PHP so far has been to include the header and footer of websites, changing the date daily, and including certain variables that I like to reserve the ability to change (like a price or an address).

    Today I was asked by a client to make certain pages on the website have different page titles. This would normally be a snap to do in HTML, but I have already set up this website using a system where every page has the same header file.

    So My question is how do I change the page title of certain pages using php? ..Either detecting the file name and changing it that way or by letting me insert a code into the body of the text that would change the title variable?

    Any help is appreciated!

    The website is here:


    There’s definitely different ways of doing this. For me, I kind of like keeping it simple.




    My Site | <?php echo $pagetitle; ?>

    Alternatively, you could have something like this in your Header.php file which checks if PageTitle is set at all and if it is, use that instead of the default "My Site"

    <br /> <?php if ($pageTitle) echo $pageTitle else echo "My Site"; ?><br />

    You could do it based on the php page name itself but it’s all preference to me.


    This didn’t work, or at least I am not executing it correctly. Maybe it is my placement? I’m getting no title when using this method.

    Let me explain more..
    Basically my pages on the website work like this:



    Is it bad that I have the doctype information all in the header.php file and could that be a reason this isn’t working?
    Also, thank you for getting back to me so quickly!


    you would need to put $pageTitle before you include the header file like this:


    You need to declare the variable before you try to use it.


    Oh wow. That is such a stupid mistake I didn’t catch.
    Thank you so much!

    Now the if statement I am having a bit of trouble with. I replace this:

    <?php echo $pagetitle; ?>

    with this:

    <?php if ($pagetitle) echo $pagetitle else echo "My Site";?>

    The homepage literally doesn’t show up once I enter the code, yet the page title is correct only on pages with a specific title.
    Can you tell what I am doing wrong?


    Oh wow, such an easy fix again. Thank you so much!
    I’ve already applied this fix to page descriptions too and it works perfectly!


Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.