include files with open {

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danneth
Forum Newbie
Posts: 2
Joined: Fri Sep 22, 2006 6:27 am

include files with open {

Post by danneth »

Hi.

Here's what I want to try. A lot of pages will need the same statements at the beginning and end, but with different "middles", like this

Code: Select all

<?php
  /* complete_page.php */
  /* header stuff */
  SOME STATEMENT
  SOME STATEMENT 
  if(TEST == VALUE){
    CONDITIONAL THINGS
  }else{
    /* page specific */
    PAGE STUFF
    PAGE STUFF
  }
  /* footer stuff */
  SOME STATEMENT
So what I thought was that I would put the stuff in a header and footer file and include them like

Code: Select all

<?php
  /* header.php */
  SOME STATEMENT
  SOME STATEMENT 
  if(TEST == VALUE){
    CONDITIONAL THINGS
  }else{
?>

Code: Select all

<?php
  /* footer.php */
  }
  SOME STATEMENT
?>
And on the pages that need it include them in the beginning and at the end

Code: Select all

<?php
  /* some_page.php */
  include("header.php");
  PAGE STUFF
  PAGE STUFF
  include("footer.php");
?>
But when I tried this I got the following error message

Code: Select all

Parse error: syntax error, unexpected $end in header.php
So my question is, is what I'm trying to do even possible, or am I just doing it wrong?
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

conditionals can't be broke up into separate files, sorry.
danneth
Forum Newbie
Posts: 2
Joined: Fri Sep 22, 2006 6:27 am

Post by danneth »

dammit..
I suspected that, oh well, I will have to figure out som other way of solving it.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

The general approach, however, is good - it is good practice to split commonly used code into separate files. I'm sure that with a bit of a logical rethink you will be able to create your header and footer files without the need to split conditionals across them.
aquanutz
Forum Commoner
Posts: 28
Joined: Thu Sep 14, 2006 9:07 am

Post by aquanutz »

I think a way you may be able to do it is have your php script and your includes, make a local variable and then assign it the resut of on of your includes, then in the included file append all the output you want onto a variable and then return that variable to the host script... something like this:

main.php

Code: Select all

include "top.php";
include "bottom.php";
$p = top();
//do other stuff in there..... and a append it to $p with the '$p .= whatever...';
$p .= bottom();
echo $p;
and in your include files just do something similar above, assign all the html to a local variable and then return that. I hope this helps you accomplish what you want to do.
Post Reply