0

My code looks like:

index.php:

<html>....  
    <title>My page</title>

....

<?php
    switch ($_GET['id']){  
        case "tips":  
            include("tips.php");  
            $title = "Tips";
...

How do I get the title varible to the html title tag?

All pages pass through index.php.

4 Answers 4

10

Do your PHP before your HTML output.

<?php

  switch ($_GET["id"]) {
    case "tips":
      $title = "Tips";
      $page  = "tips.php";
  }

?>
<html>
 <head>
  <title><?php print $title; ?></title>
 </head>
 <body>
  <?php include($page); ?>
 </body>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

No, I start with the html head. First line of PHP comes after <body>
@Johan, Johnathan's answer was a statement, not a question. He was pointing out what you SHOULD do not whether you were doing it..
@Johan, always perform logic like this before you print any HTML. That way, when you get to your HTML it isn't cluttered with PHP. Notice how much cleaner my HTML is than yours this is ideal. Hope this helps.
Don't forget the DOCTYPE to prevent quirks mode!
@Peter, that's true, but beyond the scope of this example :)
2

You can also do this with output buffering and a little string replacement.

<?php ob_start('ob_process'); ?>
<html>....  
    <title>{{{TITLE}}}</title>

....

<?php

switch ($_GET['id']){  
  case "tips":  
    include("tips.php");  
    $title = "Tips";
    break;
}

function ob_process($buffer) {
  global $title;
  $buffer = str_replace('{{{TITLE}}}', $title, $buffer);
  return $buffer;
}

5 Comments

Global variables? Output Buffering? This is over-complicating the situation.
It's an option. None of the solutions are ideal - he'd be better off using a proper framework or something that handles all this.
How is my solution not ideal? I agree that he should use a framework, but if he isn't capable yet, performing logic apart from markup is ideal. Or am I missing something?
Your situation isn't ideal because switch statements including PHP files went out when PHP-Nuke did. It works, but it's hardly pretty.
Incidentally, this solution also separates logic from markup.
0
< title > <?php echo $title; ?> < /title >

3 Comments

$title isn't declared until after the example outputs the title tags. Your answer is partly correct, but is missing some clarity on fixing the chronology of variable-declaration.
i thougt that he declares before using :)
No, that would be the best thing, but he unfortunately didn't do that. He doesn't declare it until the end of his example, yet he wants to use it at the beginning.
0

If you cannot use what Ahmet KAKICI suggests, add a script tag which sets document.title

<html>....  
    <title>My page</title>

....

<?php
    switch ($_GET['id']){  
        case "tips":  
            include("tips.php");  
            $title = "Tips";
...
?>
<script>top.document.title="<?php=$title?>"</script>

My PHP is rusty, just wanted to suggest document.title

3 Comments

I think that will hurt you when it comes to search-engine optimization.
Agree. That is why it says "If you cannot use what Ahmet KAKICI suggests"!
@Jonathan Cool you clarified :)) I did think you did. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.