Alex
Alex

Reputation: 105

Php - Title in Variable (not working)

I'm using include header in all of my php pages and want to extract each page title and description from a variable , this idea is working on php 5.4 at my local pc (wamp) , but on my host where php 5.2.17 is installed , its not showing title of any page ??

page.php:

<?php
include("header.php");
$title = "Page title";
?>

header.php:

<title><?php echo $title; ?></title>

any help please ??

Upvotes: 2

Views: 8310

Answers (4)

<?php 
    $title = 'title';
    echo '<title>'.$title.'</title>';
?>

Upvotes: 0

DorianHuxley
DorianHuxley

Reputation: 662

Your variable has not been assigned before you try and echo it in your include.

Change page.php to:

<?php
$title = "Page title";
include("header.php");
?>

(or probably move the variable $title assignment to header.php, making it more organised)

Upvotes: 0

Sam Aleksov
Sam Aleksov

Reputation: 1201

You're displaying the value of $title before the assignment.

page.php:

<?php
   $title = "Page title";
   include("header.php");
?>

header.php:

<title><?php echo $title; ?></title>

Upvotes: 9

anon
anon

Reputation:

You're trying to echo an undefined variable. The value of $title isn't declared but you're trying to echo it in header.php. You need to do it like this instead:

header.php:

<?php
$title ="Page title";
?>

page.php:

<?php
include("header.php");
<title><?php echo $title; ?></title>
?>

Hope this helps!

Upvotes: 0

Related Questions