1

I have a php include statement for the "head" of my website.

I am using the following code to call head.php...

<?php 
    include '../components/head.php' 
?>

And in my head.php I have the following code...

<head>
    <link rel="stylesheet" type="css" href="/style.css">
    <title>Dummy Code</title>
</head>

How can I make it change the title by having a variable in my page on my page like Dummy Code | About being the title if I have $title = "About" on my webpage.

Is there anyway to do this?

Dummy Code
  • 1,818
  • 4
  • 19
  • 38

1 Answers1

3

They all belong to the global namespace, so you just can do this:

<?php 
    $title = 'about';
    include '../components/head.php' 
?>

head.php:

<head>
    <link rel="stylesheet" type="css" href="/style.css">
    <title>Dummy Code | <?=$title; ?></title>
</head>

But make sure that you understand: this is very simplified code and should not be used on the production projects.

Hast
  • 9,416
  • 6
  • 45
  • 68
  • What do you mean by _this is very simplified code and should not be used on the production projects._? – Dummy Code Jun 23 '13 at 19:47
  • I mean you shuld avoid of including files with html code and use some template engines instead. – Hast Jun 23 '13 at 19:50
  • I don't understand, I am very new to PHP and HTML. – Dummy Code Jun 23 '13 at 19:53
  • Okay, a few words. It's a bad practice when PHP code is combined with HTML. Read this: http://stackoverflow.com/questions/436014/why-should-i-use-templating-system-in-php – Hast Jun 23 '13 at 19:56