Forum Moderators: coopster

Message Too Old, No Replies

Modify Code for Title Tag if No Variable is Defined?

         

clearpixel

7:29 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



I am using the code below to have a variable in the URL to reflect in the Title Tag (e.g., index.php?title=Webmaster%World would equate to Webmaster World as the title of the page). However, I am using this on a page that could be accessed without any variables defined in the URL, so how can I modify the code below to 1.) Indicate a default or 2.) Have no text display at all?

<?php echo $_GET['title']?>

PS: There is a parallel thread to this one located at [webmasterworld.com...]

alce

7:53 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



I believe you could do soemthing like this:

$title = isset($_GET['title'])?$_GET['title']:'default_title';

if the variable 'title' exists in the $_GET array, $title will take its value, if not, it will take the value you define as default.

then you just need to echo the variable

<title><?=$title?></title>

clearpixel

9:49 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Forgive me, but I have a bad case of noob-itis: where in the code would I insert this: $title = isset($_GET['title'])?$_GET ['title']:'default_title';? Does it go with the title tag or somewhere else on the page?

alce

10:06 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



You can insert it before your DOCTYPE declaration

<?php
$title = isset($_GET['title'])?$_GET['title']:'default_title';
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?=$title?></title>
</head>

<body>
</body>
</html>

clearpixel

11:10 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



Excellent - it works! Thanks!