Jump to content


Echo variable before it is defined.. Follow-up question.. NEVERMIND!


  • Please log in to reply
6 replies to this topic

#1 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 17 October 2006 - 10:40 PM

Most pages on my website are generated with three include commands, such that the header and footer are the same for all::

include 'header.php';
include "$some_unique_page.php";
include 'footer.php';

In my header, I want to have the following code::

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


..and I want the $page_title_of_unique_content variable to be defined in the $some_unique_page.php file.

Question::  Is it possible somehow echo this variable in the header title when it is being defined later on in the included script?



#2 rab

rab
  • Members
  • PipPipPip
  • Advanced Member
  • 155 posts

Posted 17 October 2006 - 10:53 PM

No, not if it is already defined

#3 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 17 October 2006 - 11:10 PM

It is not defined already, just later, in the some_unique_page.php


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 October 2006 - 11:14 PM

trying to echo a variable before it is defined would be like me expecting you to be able to solve a math problem for me without ever seeing the equation. there is no way PHP knows what to output if a variable has not been defined. why, exactly, can't the title variable be set somewhere else before the header is output?
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 18 October 2006 - 03:24 AM

Smarty templates will solve this problem.  It's a big step from standard php to Smarty though.

Another option is to buffer your output using ob_start() and related functions.  Then you can modify the buffer before displaying it, with str_replace() for example.

<? ob_start() ?>
<title>{PAGE_TITLE}</title>

...

$output = ob_get_clean();
$output = str_replace('{PAGE_TITLE}', $page_title_of_unique_content, $output);
print $output;


#6 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 18 October 2006 - 02:48 PM

This is exactly what I needed!!  Thank you so much!



#7 Guest_kilbad_*

Guest_kilbad_*
  • Guests

Posted 18 October 2006 - 05:57 PM

Follow-up question (I am new to smarty templates)..

With regard to smarty tags (like {PAGE_TITLE} for example, if I leave it undefined within some php document, how can I make it so that nothing is displayed, rather than "{PAGE_TITLE}"?

Restated, in the above example, if {PAGE_TITLE} was undefined, I would want no title displayed, NOT "{PAGE_TITLE}"..


Thanks in advance for the help!!
Brendan




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users