Displaying the current filename within HTML title tags using PHP

Primarily when I’m on a page, I refer to the page title in the tab display in my browser to remind me of which page I’m on – don’t ask me why! Obviously, I will look at the page content as well.

Recently, I’ve been brainstorming some web app ideas and I was thinking about how I see the page, one thought lead to another… You know how it goes. So it got me thinking, I wouldn’t like to have a column in my SQL table where I have to put in the title for my <title> tag.

What if there was something quicker than going through database connections and all that fun stuff?

Well, I came up with something like this:

  • Create a function 
  • Create an if statement to
    • Display specific title on the index page
    • Display another title on all other pages
  • Call our function within the <title> tags

I did this by getting the name of the file:

<?php echo basename(__FILE__, '.php'); ?>

Essentially, what the above snippet does is, it gets the trailing path of the current file and removes the .php file extension. But, by all means, if you wanted to keep the file extension then you’d have to leave out adding the .php remover:

<?php echo basename(__FILE__); ?>

Now we’ve covered how the logic of the code works, let’s jump into some in depth code.

<?php

function theTitle(){ // Create the function
    if( basename(__FILE__) == "index.php" ) { // Create the if statement
        echo "Dashboard | App Name"; // Display title if page is index.php
    } else { // Or else
        echo (ucfirst(basename(__FILE__, '.php'))) . "&nbsp;| App Name"; // Display other page title
    } // Close if statement
} // Close function

?>

See my code comments, then read the bullets:

  • Creating the function and calling it theTitle
  • Creating the if statement, if on index.php then…
  • Display a UNIQUE page title because it’s the root page
  • Else – if it’s not the index.php / root page, then…
  • Display the current filename, with a capital at the beginning and remove the file extension
  • Close if statement
  • Close function
<?php ucfirst(); ?>

The above code displays a strings first character as a capital letter.

Now the function is in place, I called the function in my <title> tags, in the <head>:

<title><?php theTitle(); ?></title>

What I just did there is call theTitle which is the function I created earlier.

That’s pretty much it, here’s my full code:

<?php

function theTitle(){ // Create the function
    if( basename(__FILE__) == "index.php" ) { // Create the if statement
        echo "Dashboard | App Name"; // Display title if page is index.php
    } else { // Or else
        echo (ucfirst(basename(__FILE__, '.php'))) . "&nbsp;| App Name"; // Display other page title
    } // Close if statement
} // Close function

?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title><?php theTitle(); ?></title>
    </head>
    <body>
        <p>Body content.</p>
    </body>
</html>

NOTE: I have 2 pages in my demo that I used to create this tutorial, index.php and other.php – they both use the same code, so I’m not going to include 2 snippets below for the two pages.

I hope this can help you guys, it’s a really cool way without having to use databases and all that confusing stuff.

  • I guess finding useful, reliable iniotmarfon on the internet isn’t hopeless after all.