views:

57

answers:

3

How can I reference variables from an included file before it's been included? Or can I somehow include the file (so I can lead its variables later) before its HTML is literally inserted into the body tag? Or can I contain all of home's body content in one big variable that I can echo as well in the index?

Here's what I'm trying to do:

index.php

<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>

<?php include 'home.php'; ?>

</body>
</html>


home.php

<?php
$title="home page";
$description="this is the home page"; 
$keywords="home, awesome, yes";
?> 

this is the home page content that gets inserted into the body!
A: 

Hi,

Just move the include statement to the top of the file. This will expose all values, functions and variables to all subsequent lines.

<?php include 'home.php'; ?>
<html>
<head>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $description; ?>" />
<meta name="keywords" content="<?php echo $keywords; ?>" />
</head>
<body>



</body>
</html>
John Ballinger
Okay, so would I then make the entirety of the main content in home.php into one long variable?
Adam Hunter Peck
YES. You can put all your variables in HOME.PHP, make sure you include this at the top of you file.
John Ballinger
A: 

Short answer version: You can't. You'll get an 'Undefined variable' notice if you do that.

I find it is usually much more convenient to have a header.php (and a footer.php for that matter) which gets included in the index, home, contact or whatever other file. The advantage is that you don't have redundant code, and if you need to make a modification in the header or footer, you need to only modify one file.

So for example, 'about_us.php' would look like:

<?php
include('path/to/header.php');
#body goes here
include('path/to/footer.php');
?>

And your header would be something like:

<?php
$title = ucfirst(str_replace('_', ' ', substr(basename($_SERVER['PHP_SELF']), 0, -4));
?>
<html>
    <head>
        <title><?php echo $title; ?> page</title>
        <meta name="description" content="this is the home page" />
        <meta name="keywords" content="home, awesome, yes" />
    </head>
    <body>

The $title variable will be the file name, minus the extension, with all underscores replaced by spaces and the first letter of the first word capitalized. So basically about_us.phpwould be converted into "About us". This is not necessarily a general solution, but I gave it as an example keeping in mind that you wanted to use a dynamic title in your original example. For dynamic description and keywords, based on the file name you could also assign different values with the help of a switch() statement.


UPDATE:

Another solution, although kind of the reverse of what you're asking, but at the same time much closer to what you're looking for would be to write the header.php like

<html>
    <head>
        <title><?php echo $title; ?> page</title>
        <meta name="description" content="<?php echo $desc; ?>" />
        <meta name="keywords" content="<?php echo $keywords; ?>" />
    </head>
    <body>

... the footer like ...

    </body>
</html>

... and then include them in your other files:

<?php
$title = 'Your title';
$desc = 'Your description';
$keywords = 'The, big, brown, fox, jumps, over, the, lazy, dog';
include('path/to/header.php');
?>

<!-- body goes here -->

<?php
include('path/to/footer.php');
?>

This way, you are assigning all the variables BEFORE you are including the files in which they are being referenced, you have distinct files for all the links and you don't need fancy switches. Also as a side note, wrapping the body's HTML in PHP is simply bad practice. Try to keep the HTML separated from the PHP as much as possible in general. It will help both you, and whoever is going to do work on the code in the future.

Hope this helps !

FreekOne
That's a great solution, and it'll definitely be my fall back, but I'm still curious if there's a way that you can have one main template file, and then individual child page files which contain their own header data (title, meta tags) as well as body content.
Adam Hunter Peck
You could use John Ballinger's solution and store the body content in a variable, but you would then need to send a variable to the index file (see [`$_GET`](http://php.net/manual/en/reserved.variables.get.php) ), and use it in a `switch()` statement to let the script know WHICH page should it should actually include and display. I am not sure however how healthy it is to have only one base file for all links (i.e. index.php?do=home or index.php?do=about_us etc) and what that means for the different keywords and descriptions since I don't fiddle much with SEO.
FreekOne
Please see my updated answer, I believe it is much closer to what you're trying to achieve.
FreekOne
A: 

I would have a look at using a template system. Separating your code from the content will save you a lot of trouble in the future. it will also allow you to change the html template easily in the future. plus you can see your template without having to run the php code.

have a look at smarty templates http://www.smarty.net/

you would then build a template file: "template.tpl"

<html>
  <head>
    <title>{$title}</title>
    <meta name="description" content="{$description}" />
    <meta name="keywords" content="{$keywords}"/>
  </head>
  <body>
    {$home_content}
  </body>
</html>

and some php code to run:

<?php
require_once('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('title'        , 'Your title');
$smarty->assign('description'  , 'Your description');
$smarty->assign('keywords'     , 'The, big, brown, fox, jumps, over, the, lazy, dog');
$smarty->assign('home_content' , 'this is the home page content that gets inserted into');
$smarty->display('template.tpl');
?> 

And that is just scratching the surface of what a templating system can do. you can repeating or optional bocks, include other templates, etc etc.

Bingy