簡體   English   中英

解析錯誤:語法錯誤,意外的“;”,需要標識符(T_STRING)或變量(T_VARIABLE)

[英]Parse error: syntax error, unexpected ';', expecting identifier (T_STRING) or variable (T_VARIABLE)

我的代碼有問題。 (我已經尋找了一些解決方案,但我沒有找到任何東西,所以這就是我發布主題的原因)

錯誤:

Parse error: syntax error, unexpected ';', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in XXXXXX on line 13

第 13 行:

echo $view->;htmlError();

我的代碼:

<?php
require_once 'classes/View.php';

echo DB_HOSTNAME; //output: hostname

$view = new View();

if ($htmlString = $view->tableThreads()) {
    echo $htmlString;
} else {
    echo $view->;htmlError();
}

echo $view->buttonPostThread();
?>

查看.php

// we need the class db to make an object
require_once 'database.php';

//we'll also need the recaptcha helper later
require_once 'helpers/recaptcha.php';

class View{

    private $db;

    function __construct()
    {
        $this->db = new Database();
    }

    function tableThreads()
    {
        $content = "";

        if (($threads = $this->db->getThreads()) && mysql_num_rows($threads) > 0) {
             $content .= '<h1>Threads</h1>';
             $content .= '<table border="0" width="" id="posts_list">';
             $content .= '<tr>';
             $content .= '<th class="title">Title</td>';
             $content .= '<th>Date</td>';
             $content .= '<th>User</td>';
             $content .= '</tr>';

            while ($row = mysql_fetch_assoc($threads)) {
                $content .= '<tr class="thread">';
                $content .= '<td class="title">';
                $content .= '<a href="view_thread.php?permalink=';
                $content .= htmlspecialchars($row['permalink']) . '">'.$row['title'].'</a>';
                $content .= '</td>';
                $content .= '<td class="date">'.htmlspecialchars($row['date']).'</td>';
                $content .= '<td class="author">'.htmlspecialchars($row['author']).'</td>';
                $content .= '</tr>';
            }
            $content .= '</table>';
            return $content;
        } else {
            return false;
        }
    }

private function composeTable($post, $firstPost, $numRows)
{
    $htmlTable = "";

    if ($firstPost)
        $htmlTable .= '<h1>'.htmlspecialchars($post['title']).'</h1>';

    $htmlTable .= '<table border="0" width="895">';
    $htmlTable .= ' <tr>';
    $htmlTable .= '     <th>Message</th>';
    $htmlTable .= '     <th>Date</th>';
    $htmlTable .= '     <th>Author</th>';
    $htmlTable .= ' </tr>';
    $htmlTable .= ' <tr>';
    $htmlTable .= '     <td class="title">'.htmlspecialchars($post['content']).'</td>';
    $htmlTable .= '     <td class="date">'.htmlspecialchars($post['date']).'</td>';
    $htmlTable .= '     <td class="author">'.htmlspecialchars($post['author']).'</td>';
    $htmlTable .= ' </tr>';
    $htmlTable .= '</table>';
    if ($firstPost && $numRows &gt; 1)
        $htmlTable .= '<h1>Responses</h1>';

    return $htmlTable;
}

function tableThreadContent($permalink)
{
    $content = "";

    if ($posts = $this->db->getContentThread($permalink)) {
        $num_rows = mysql_num_rows($posts);
        if ($num_rows > 0) {
            while($row = mysql_fetch_assoc($posts))
                $content .= $this->composeTable($row,
                    is_null($row['permalink_parent']),
                    $num_rows);
        }
        return $content;
    } else {
        return false;  //database error
    }
}

[/html]

<p>The second method goes around all the posts in a thread and composes the HTML with the first one (composeTable). </p>

<p>The method composeTable is private because we'll only call it from the tableThreadContent method in the same class and its functionality is only useful inside this class. In the future if we want to make a class that extends this one and uses that method all we need to do is change private for protected.</p>

<p>Now let's think about what happens if we don't have a single thread. Apart from being very sad it could be a problem if we don't show a warning message. This is a very simple method to do that:</p>

function htmlError($from_view_thread = false)
{
    if ($from_view_thread) {
        //From view_thread.php
        $html = '<p class="error">There is no thread with this title. Sorry! ';
        $html .= 'You can go back to <a href="index.php">the main page</a>.</p>';
    }else{
        // From index.php
        $html = '<p class="error">There aren\'t any threads. Sorry! </p>';
    }
    return $html;
}

function buttonPostThread()
{
    return '<div class="newThread">'
          .'<a href="post_message.php">Create a new thread</a>'
          .'</div>';
}

您需要更正該行:

echo $view->;htmlError();

到:

echo $view->htmlError();

為了休息,請檢查您的代碼。

您必須將第 13 行替換為:

echo $view->html Error();

因為你有一個額外的“;”

在 PHP 5.5 及以上

$this->middlewares[$k] = Instance::ensure($middleware, Middleware::class);

在 PHP 5.5 下面

$this->middlewares[$k] = Instance::ensure($middleware, 'Middleware');

PHP 5.4 不支持 Class::class

暫無
暫無

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

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