hoolakoola hoolakoola - 10 days ago 5
HTML Question

How to create dynamic HTML<title> tag based on page name with PHP?

How would I dynamically change my HTML title tag based on the .php/.html file name? Right now I am using a switch case to try and accomplish this, but I am not sure how to get the page name in order to compare it for each case. Here is my PHP code:

$prefix = 'MySiteTitle | ';

switch ($_GET['PHP_SELF']){
case 'www.mysite.php/home':
$pageTitle = 'Home';
}


And in my title tag, it goes:

<title><?php echo $prefix . $pageTitle ?></title>


This has not worked so far, as the $pageTitle variable is empty

dpp dpp
Answer

You might mean $_SERVER['PHP_SELF'] and not $_GET['PHP_SELF'] (though you could've named your parameter that). Check that output

echo $_SERVER['PHP_SELF'] 

and make sure that echo matches 'www.mysite.php/home'

$prefix = 'MySiteTitle | ';

switch ($_GET['PHP_SELF']){
    case 'www.mysite.php/home':
        $pageTitle = 'Home';
     default:
         $pageTitle ='You are here';
}