Display Outside the Loop

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
psaha
Forum Newbie
Posts: 16
Joined: Mon Oct 26, 2009 4:11 am

Display Outside the Loop

Post 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 ....
?>
-------------------------------------------
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Display Outside the Loop

Post 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....
 
psaha
Forum Newbie
Posts: 16
Joined: Mon Oct 26, 2009 4:11 am

Re: Display Outside the Loop

Post 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?
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Display Outside the Loop

Post 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
Post Reply