• 0

PHP question


Question

all right, i know very little about php, so my knowledge is very sparse and scattered.. but here's the scenario. i have a very simple global page layout template system powered by php. it works like this: i have one file called global_template.inc containing parts of the site layout with variable values in it. simplified example of the file global_template.inc:

<?function commonHeader($pagetitle, $yearmonth)
{ ?>
<html>
<html>
<head>
	<title><?echo $pagetitle;?></title>
</head>

<body>
<a href="<?echo $yearmonth;?>/test.php3">Test Link in Header</a>

<? }
function commonFooter($yearmonth) { ?>

<a href="<?echo $yearmonth;?>/test.php">Test Link in Footer</a>
</body>
</html>
<? } ?>

and then this would be a simple example of one of my actual pages:

<? require("global_template.inc");
commonHeader("This is the page title","2003/01"); ?>

<p>This is my page body.</p>

<? commonFooter("2003/01"); ?>

All right. Please notice the "yearmonth" variable and how both the commonHeader and commonFooter have it in global_template.inc, and how i have to declare it twice in one of the actual pages. Is there any way to get both commonHeader and commonFooter to SHARE one declaration of this variable, so I only have to enter it once and have them both take it happily?

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

A very simple way would be to put the yearmonth value into a variable and pass the variable to each function - like this:

<?php require("global_template.inc");
$yearmonth = "2003/01";
commonHeader( "This is the page title", $yearmonth ); ?>
<p>This is my page body.</p>
<?php commonFooter( $yearmonth ); ?>

The other way would be to set it to be a global variable - so all functions can use it without it being passed to them. Like this...

Page:

<?php require("global_template.inc");
$yearmonth = "2003/01";
commonHeader( "This is the page title" ); ?>
<p>This is my page body.</p>
<?php commonFooter(); ?>

<?php
function commonHeader($pagetitle, $yearmonth)
{
global $yearmonth;
?>
<html>
<html>
<head>
<title& #62;<?echo $pagetitle;?></title>
</head>
<body>
<a href="<?echo $yearmonth;?>/test.php3">Test Link in Header</a>
<?php
}
function commonFooter($yearmonth) {
global $yearmonth;
?>
<a href="<?echo $yearmonth;?>/test.php">Test Link in Footer</a>
</body>
</html>
<?php
}
?>

Edited by Quboid
Link to comment
Share on other sites

  • 0

Which one did you go for and did it work?

I'm kinda curious about the second one as I've never used it before. I got that from my PHP book without testing it so I could have picked up something incorrectly.

Link to comment
Share on other sites

  • 0

quick note when coding: use <?php versus <?. It's a standards thing, and php *may* stop supporting it at some point (probably not, but it's possible). Plus if another language comes out to run at the same time as php, using the same delimiters, how are they going to know which is which? :)

Link to comment
Share on other sites

  • 0

But, <?php is the standard. And the .php doesn't protect the file from other apache modules parsing it if they want...

Anyhoo, I'm just saying, as it makes code cleaner in terms of showing that this code block is definitely php :)

Edit: Forgot about XML! It'll get confused with PHP. This is a problem. Read the bitching from the php devs here: http://news.php.net/article.php?group=php....v&article=89446

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.