I am trying to output the page title dynamically. I am using induces and this script is withing the header.php the goal is to output the header dynamically using a case/switch statement. here is my code:
<?php $title ; switch($_SERVER['PHP_SELF']) { case '/index.php': $title = 'Home'; break; case '/about.php': $title = 'About'; break; case '/services.php': $title = 'Services'; break; case '/portfolio.php': $title = 'Portfolio'; break; case '/staff.php': $title = 'Staff'; break; case '/contact.php': $title = 'Contact us'; break; } ?> <title><?php echo $title ?></title>
I am getting a error telling me my variable $title is not defined?
What i am doing wrong?
Advertisement
Answer
In your first line, you have
<?php $title ;
This $title ;
shouldn’t be there.
And, as Kailash Ahirwar already mentioned, it’s always a good idea to provide a default value for your $title
:
switch($_SERVER['PHP_SELF']) { [...] default: $title = "Default title goes here"; }