Page 1 of 1

Display Outside the Loop

Posted: Mon Nov 16, 2009 2:33 pm
by psaha
I am trying to display rather hold the value of $title outside the loop.
It works while I put echo $title below the ?> php code.. but not above. I want it globally and put into <title> tag.
How can I do that? Please help.....
-------------------------------------------------
<html>
<head><title><? echo $title; ?></title></head>
<body>
bla bla bla....

<?php
other $php variables...
explode(); bla bla..
foreach($values as $key)
{
$title=something ;
}
echo ....
?>
-------------------------------------------

Re: Display Outside the Loop

Posted: Tue Nov 17, 2009 6:57 am
by iankent
You can't do it that way around since the PHP parser will echo the blank value of $title because it hasn't yet been filled. Try it this way around:

Code: Select all

 
<?php
other $php variables...
explode(); bla bla..
foreach($values as $key)
{
$title=something ;
}
?>
<html>
<head><title><? echo $title; ?></title></head>
<body>
bla bla bla....
 

Re: Display Outside the Loop

Posted: Tue Nov 17, 2009 8:11 am
by psaha
iankent wrote:You can't do it that way around since the PHP parser will echo the blank value of $title because it hasn't yet been filled. Try it this way around:

Code: Select all

 
<?php
other $php variables...
explode(); bla bla..
foreach($values as $key)
{
$title=something ;
}
?>
<html>
<head><title><? echo $title; ?></title></head>
<body>
bla bla bla....
 

It will not work for me Because.. the
<?php
It self echo something which will be in the body section NOT above the <head>
?>

any other idea? as I can hold the value above the <head> section and display in body section?

Re: Display Outside the Loop

Posted: Tue Nov 17, 2009 8:17 am
by iankent
You really have no other option - if you want a variable to be output inside the <title> tags, it must be defined AND filled before the <title> tag is output. There's nothing that can be done about that.

<?php itself doesn't output anything, so there's no reason that can't go above the <html> tag. You just need to split your PHP code into:
- parts that do stuff but output nothing - can go at the top
- parts that do nothing but output stuff - must go within the page
- parts that do stuff and output stuff - must go within the page

The line that sets the value of $title can go at the very top as it outputs nothing. The line that echo's the $title variable obviously has to go inside the <title> tags. Without seeing all of your code its impossible to say how you could reorganise it to achieve what you're after, but nonetheless, the line of code that fills the $title variable must go before the <title> tags, else you get a blank title.

Alternatively, you could make use of Javascript to assign a value to the <title> tags after the page has been fully loaded and rendered, but that's not a very good way to go about it as some users wont have javascript, and if your page takes ages to load, your title will remain blank until it finishes.

hth