为什么我的变量未定义?

时间:2012-12-03 23:06:07

标签: php

我注意到这是php中的一个常见错误,我试图搜索网络,我无法得到我的答案,我正在尝试创建一个simpe论坛,我收到这些错误

Notice: Undefined variable: topics in C:\xampp\htdocs\mysite\Students forum\view_category.php on line 83

Notice: Undefined variable: topic in C:\xampp\htdocs\mysite\Students forum\view_category.php on line 86

这是第83行的代码

$topics .= "<table width='100%' style='border-collapse: collapse;'>";

这是第86行的代码

$topic .= "<tr><td colspan='4'><hr /></td></tr>";

这是我的代码

if (mysql_num_rows($res2) > 0) {
        // Appending table data to the $topics variable for output on the page
        $topics .= "<table width='100%' style='border-collapse: collapse;'>";
        $topics .= "<tr><td colspan='4'><a href='index.php'>Return To Forum Index</a>".$logged."<hr /></td></tr>";
        $topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Last User</td><td width='65' align='center'>Replies</td><td width='65' align='center'>Views</td></tr>";
        $topic .= "<tr><td colspan='4'><hr /></td></tr>";
        // Fetching topic data from the database
        while ($row = mysql_fetch_assoc($res2)) {
            // Assign local variables from the database data
            $tid = $row['id'];
            $title = $row['topic_title'];
            $views = $row['topic_views'];
            $date = $row['topic_date'];
            $creator = $row['topic_creator'];
            // Check to see if the topic has every been replied to
            if ($row['topic_last_user'] == "") { $last_user = "N/A"; } else { $last_user = getusername($row['topic_last_user']); }
            // Append the actual topic data to the $topics variable
            $topics .= "<tr><td><a href='view_topic.php?cid=".$cid."&tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted by: ".getusername($creator)." on ".convertdate($date)."</span></td><td align='center'>".$last_user."</td><td align='center'>".topic_replies($cid, $tid)."</td><td align='center'>".$views."</td></tr>";
            $topics .= "<tr><td colspan='4'><hr /></td></tr>";
        }
        $topics .= "</table>";
        // Displaying the $topics variable on the page
        echo $topics;
    } 

4 个答案:

答案 0 :(得分:4)

运算符.=通常用于连接左右参数,但在这种情况下,您没有左参数。您必须像这样实例化变量:

$topics = "<table width='100%' style='border-collapse: collapse;'>";
$topic = "<tr><td colspan='4'><hr /></td></tr>";

if( ... ) {
    while( ... ) {
        $topics .= ...
        $topic .= ...
    }
}

答案 1 :(得分:1)

在使用$topics .="anything"

之前先声明变量
$topics =''; 
$topics .= "whatever you want"; // must have been declared before

所以在这种情况下,它可能就像

if (mysql_num_rows($res2) > 0) {
    $topics="<table width='100%' style='border-collapse: collapse;'>"
    $topics .= "<tr><td colspan='4'><hr /></td></tr>";
    // rest of your code
}

答案 2 :(得分:1)

$topics .=$topics = $topics .的简写,如果您在第一次使用变量时使用该变量,则该变量不会被视为“已定义”。

所以你至少应该这样做:

$topics = '';
$topic = '';

开始使用速记之前。

答案 3 :(得分:0)

$topic .= 'something';

相同
$topic = $topic . 'something';

它实际上意味着:

  1. 采取$ topic,
  2. 加入“背后的东西”
  3. 将结果放回$ topic
  4. 如果$ line未在此行之前的任何位置使用,则第一步失败,因为$ topic变量尚不存在。

    第一次在$ topic中添加内容时,请使用简单的

    =
    

    相反,您可以通过

    加入更多内容
    .=
    
相关问题