首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的变量没有定义?

问为什么我的变量没有定义?
EN

Stack Overflow用户
提问于 2012-12-04 07:06:07
回答 4查看 300关注 0票数 0

我注意到这是php中的一个常见错误,我试图在网上搜索,但找不到答案,我正在尝试创建一个simpe论坛,我得到了这些错误

代码语言:javascript
复制
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行的代码

代码语言:javascript
复制
$topics .= "<table width='100%' style='border-collapse: collapse;'>";

这是第86行的代码

代码语言:javascript
复制
$topic .= "<tr><td colspan='4'><hr /></td></tr>";

这是我的代码

代码语言:javascript
复制
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;
    } 
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-12-04 07:09:30

运算符.=通常用于连接右参数和左参数,但在本例中,您没有左参数。必须按如下方式实例化该变量:

代码语言:javascript
复制
$topics = "<table width='100%' style='border-collapse: collapse;'>";
$topic = "<tr><td colspan='4'><hr /></td></tr>";

if( ... ) {
    while( ... ) {
        $topics .= ...
        $topic .= ...
    }
}
票数 4
EN

Stack Overflow用户

发布于 2012-12-04 07:10:37

在使用$topics .="anything"之前首先声明变量

代码语言:javascript
复制
$topics =''; 
$topics .= "whatever you want"; // must have been declared before

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

代码语言:javascript
复制
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
}
票数 1
EN

Stack Overflow用户

发布于 2012-12-04 07:11:13

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

所以你至少应该这样做:

代码语言:javascript
复制
$topics = '';
$topic = '';

在你开始使用速记之前。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13693388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档