soniccool
soniccool

Reputation: 6058

Title Name Page PHP confusion

So lets say i have a php file that contains

$title="Website | Categories";

And my header.inc file includes <title><?=$title?></title> which will output the title name.

But lets say i also have a variable on my php page called 'cat_name'

How can i add 'cat_name' into $title="Website | Categories"; Would it be like $title="Website | Categories 'cat_name'";

Upvotes: 0

Views: 109

Answers (3)

David Grenier
David Grenier

Reputation: 1241

These will work:

  • $title = "Website | Categories " . $cat_name;
  • $title = "Website | Categories $cat_name";

This will not work:

  • $title = 'Website | Categories $cat_name';

If a string is in double-quotes it will parse any variables inside of it. However, if the string is in single quotes it will not.

If you want to get a little fancier there are various ways to append the category name with another vertical pipe, like Website | Categories | My Category.

The simplest is probably using the ternary operator to detect if $cat_name is set and append text to the $title string.

$title = "Website | Categories";
$title .= (isset ($cat_name)) ? " | $cat_name" : "";

You could also create an array and add each element of your navigation to it (Website, Category, cat_name, sub_cat, sub_sub_cat, ...) then use an implode statement to turn it into a string.

Upvotes: 2

FrankieTheKneeMan
FrankieTheKneeMan

Reputation: 6800

$title="Website | Categories $cat_name";

Upvotes: 3

Teena Thomas
Teena Thomas

Reputation: 5239

$title="Website | Categories " . $cat_name;

Upvotes: 4

Related Questions