shaytac
shaytac

Reputation: 4004

Function within echo problem

I have a slight problem with the echo statement outputting wrongly. Forexample when i do

echo "<div id=\"twitarea\">" . fetchtwitter($rss) . "</div>";

it displays function output but OUTSIDE of twitarea div. What is the cause of this behavior perhaps syntax?

thanks in advance

Here is the actual function

require_once('includes/magpie/rss_fetch.inc');
$rssaldelo = fetch_rss('http://twitter.com/statuses/user_timeline/12341234.rss');
function fetchtwitter($rsskey){
foreach ($rsskey->items as $item) {
    $href = $item['link'];
    $title = $item['title'];
    print "<li class=\"softtwit\"><a href=" . $href . "    target=\"_blank\">$title</a></li><br>";
    } }

Upvotes: 0

Views: 475

Answers (5)

Matthieu Napoli
Matthieu Napoli

Reputation: 49533

Simply :

<?php
echo "<div id=\"twitarea\">";
fetchtwitter($rss);
echo "</div>";
?>

fetchtwitter($rss) is echoing output (it doesn't return it).

With that you don't have to modify fetchtwitter().

Upvotes: 3

markcial
markcial

Reputation: 9323

you could try delimiters maybe it helps

$twitter = fetchtwitter($rss);
ob_start();
echo <<<HTML;
<div id="twitarea">$twitter</div>
HTML;
echo ob_get_clean();

update You can modify your function like this too

require_once('includes/magpie/rss_fetch.inc');
$rssaldelo = fetch_rss('http://twitter.com/statuses/user_timeline/12341234.rss');
function fetchtwitter($rsskey){ 
  $bfr =""; 
  foreach ($rsskey->items as $item){
    $href = $item['link'];
    $title = $item['title'];
    $bfr .= "<li class=\"softtwit\"><a href=" . $href . "> target=\"_blank\">$title</a></li><br>";
  } 
  return $bfr; 
}

Upvotes: 0

Abs
Abs

Reputation: 57916

In case you didn't see my comment.

Try using a return in your fetchtwitter() function rather than the echo that you have in there.

Upvotes: 0

Andy Shellam
Andy Shellam

Reputation: 15535

Does fetchtwitter(...) write the output directly to the browser instead of returning it? Try something like:

<?php

ob_start();
fetchtwitter($rss);
$twitter = ob_get_clean();

echo "<div id=\"twitarea\">" . $twitter . "</div>";

?>

Or if you can modify the source of fetchtwitter(), get it to concatenate and return the string instead of echoing it.

Upvotes: 1

Pekka
Pekka

Reputation: 449385

fetchtwitter() probably does an echo() of its own, instead of returning the string. The function is executed while echo prepares the whole string for output, before the string is printed.

Upvotes: 1

Related Questions