簡體   English   中英

PHP腳本上的未定義變量

[英]Undefined variable on php script

我在第30行上注意到twetout是未定義變量

    <?php 
    $username = "tomaskutaj";/*
    $limit = 5;
    $feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;*/
    $feed ="http://search.twitter.com/search.rss?q=@tomaskutaj";
    $tweets = file_get_contents($feed);

        $tweets = str_replace("&", "&", $tweets);   
        $tweets = str_replace("<", "<", $tweets);
        $tweets = str_replace(">", ">", $tweets);
        $tweet = explode("<item>", $tweets);
    $tcount = count($tweet) - 1;

for ($i = 1; $i <= $tcount; $i++) {
    $endtweet = explode("</item>", $tweet[$i]);
    $title = explode("<title>", $endtweet[0]);
    $content = explode("</title>", $title[1]);
        $content[0] = str_replace("&#8211;", "&mdash;", $content[0]);

        $content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $content[0]);
        $content[0] = str_replace("$username: ", "", $content[0]);
        $content[0] = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $content[0]);
        $content[0] = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $content[0]);
    $mytweets[] = $content[0];
}
$x=1;
while (list(, $v) = each($mytweets)) {
    $tweetout .= "<div>$v</div>\n";
    if ($x==1){
    $first=$tweetout;
    };
    $x++;
}
if ((strstr($first,'#tomaskutaj'))&&(strstr($first,'@tomaskutaj')))
echo($first);
  ?>

但是在我最后說“ $ tweetout =”之后添加; 它運行腳本,但沒有得到任何輸出,也沒有錯誤,問題出在哪里?

問題是您試圖追加到未定義的變量。

$tweetout .= "<div>$v</div>\n";

PHP會向您發出通知,因為在大多數情況下,發生這種情況是因為您鍵入了變量名。 您不會得到任何錯誤,因為PHP只是假設您要追加到null並繼續。

要解決此問題,請在while循環之前定義$tweetout

$tweetout = '';

您絕對應該添加一個

$tweetout = ""; 

在循環之前,附加到不存在的字符串是錯誤的。

屏幕上什么都沒顯示的原因是$ first不包含#tomaskutaj,並且結尾處的if語句要求(和那里的@tomaskutaj)打印任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM