Php 分析错误:语法错误,意外'';,应为标识符(T_字符串)或变量(T_变量)

Php 分析错误:语法错误,意外'';,应为标识符(T_字符串)或变量(T_变量),php,Php,我的代码有问题。(我已经寻找了一些解决方案,但我什么也没找到,所以我要发布一个主题) 错误: 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 'cl

我的代码有问题。(我已经寻找了一些解决方案,但我什么也没找到,所以我要发布一个主题)

错误:

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();
?>

View.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>';
}
//我们需要类db来创建一个对象
需要_once“database.php”;
//稍后我们还需要recaptcha助手
需要一次“helpers/recaptcha.php”;
类视图{
私人$db;
函数_u构造()
{
$this->db=newdatabase();
}
函数tableThreads()
{
$content=“”;
if($threads=$this->db->getThreads())&&mysql\u num\u rows($threads)>0){
$content.='Threads';
$content.='';
$content.='';
$content.='Title';
$content.=“日期”;
$content.='User';
$content.='';
while($row=mysql\u fetch\u assoc($threads)){
$content.='';
$content.='';
$content.='';
$content.='';
$content.=''.htmlspecialchars($row['date'])。';
$content.=''.htmlspecialchars($row['author'])。';
$content.='';
}
$content.='';
返回$content;
}否则{
返回false;
}
}
可组合的私有函数($post、$firstPost、$numRows)
{
$htmlTable=“”;
如果($firstPost)
$htmlTable.=''.htmlspecialchars($post['title'])。';
$htmlTable.='';
$htmlTable.='';
$htmlTable.='Message';
$htmlTable.=“日期”;
$htmlTable.='Author';
$htmlTable.='';
$htmlTable.='';
$htmlTable.=''.htmlspecialchars($post['content'])。';
$htmlTable.=''.htmlspecialchars($post['date'])。';
$htmlTable.=''.htmlspecialchars($post['author'])。';
$htmlTable.='';
$htmlTable.='';
如果($firstPost&&$numRows 1)
$htmlTable.='Responses';
返回$htmlTable;
}
函数tableThreadContent($permalink)
{
$content=“”;
如果($posts=$this->db->getContentThread($permalink)){
$num_rows=mysql_num_rows($posts);
如果($num_rows>0){
while($row=mysql\u fetch\u assoc($posts))
$content.=$this->composeTable($row,
为空($row['permalink\u parent']),
$num_行);
}
返回$content;
}否则{
返回false;//数据库错误
}
}
[/html]
第二种方法在一个线程中遍历所有帖子,并用第一种方法(composeTable)组合HTML

composeTable方法是私有的,因为我们只从同一个类中的tableThreadContent方法调用它,它的功能只在这个类中有用。将来,如果我们想创建一个扩展这个类并使用该方法的类,我们所需要做的就是将private更改为protected

现在,让我们考虑一下如果没有一个线程会发生什么。除了非常悲伤之外,如果我们没有显示警告消息,这可能是一个问题。这是一个非常简单的方法:

函数htmlError($from\u view\u thread=false) { 如果($from_view_thread){ //从view_thread.php $html='

没有具有此标题的线程。抱歉!'; $html.='您可以返回。

'; }否则{ //从index.php $html='

没有任何线程。对不起!

'; } 返回$html; } 函数按钮PostThread() { 返回“” .'' .''; }
您需要更正该行:

echo $view->;htmlError();
致:


其余部分,请检查您的代码。

您必须将第13行替换为:

echo $view->html Error();
因为在PHP5.5及更高版本中有一个额外的“;”

$this->middlewares[$k] = Instance::ensure($middleware, Middleware::class);
以及下面的PHP5.5

$this->middlewares[$k] = Instance::ensure($middleware, 'Middleware');
PHP5.4不支持Class::Class

如果错误为“意外”;“在第13行”,您需要逐个查看第13行中的所有分号,并问自己“这个分号属于这里吗?”A帮助。
$this->middlewares[$k] = Instance::ensure($middleware, 'Middleware');