HEREDOC意外结束 [英] HEREDOC Returning unexpected end

查看:95
本文介绍了HEREDOC意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段导致了

"PHP解析错误:语法错误,/Applications/MAMP/htdocs3/nettuts/PHP/PDO中意外的$ end用于数据库访问/htdocs/view_users02.php,第39行"

我环顾了网站和google,但没有找到确切的解决方案.

  foreach($DBH->query($sql) as $row){

        $output = "<tr><td align='left'>" . $row["name"] . "</td><td align='left'>" . $row["dr"] . "</td></tr>";


            // echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
     echo <<<EOT
            $output
    EOT;         


完整脚本

<?php 
$page_title = 'View the Current Users';
include ('includes/header.html');

// Page header:
echo '<h1>Registered Users</h1>';

require_once ('../mysql_pdo_connect.php'); // Connect to the db.

// Make the query:

$sql = "SELECT CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr FROM users ORDER BY registration_date ASC";   

    // Table header.
    echo <<<EOT
    <table align='center' cellspacing='3' cellpadding='3' width='75%'>
    <tr><td align='left'><b>Name</b></td><td align='left'><b>Date Registered</b></td></tr>
EOT;


foreach($DBH->query($sql) as $row){

    $output = "<tr><td align='left'>" . $row["name"] . "</td><td align='left'>" . $row["dr"] . "</td></tr>";


        // echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
 echo <<<EOT
        $output
EOT;         

        }

    echo '</table>'; // Close the table.
    $DBH = null;


include ('includes/footer.html');
?>

解决方案

循环中的EOT;后有一堆空格(准确地说是9个空格).

手册

...分号之前或之后可能没有空格或制表符...

为什么要将$output变量包装在HEREDOC字符串中?我只是将循环更改为

printf('<tr><td align="left">%s</td><td align="left">%s</td></tr>',
       htmlspecialchars($row["name"]),
       htmlspecialchars($row["dr"]));

甚至更好,请使用 PHP的替代语法. /p>

The following snippet is causing an

"PHP Parse error: syntax error, unexpected $end in /Applications/MAMP/htdocs3/nettuts/PHP/PDO for Database Access/htdocs/view_users02.php on line 39"

I've looked around the site and google but didn't find an exact solution.

  foreach($DBH->query($sql) as $row){

        $output = "<tr><td align='left'>" . $row["name"] . "</td><td align='left'>" . $row["dr"] . "</td></tr>";


            // echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
     echo <<<EOT
            $output
    EOT;         


Complete Script

<?php 
$page_title = 'View the Current Users';
include ('includes/header.html');

// Page header:
echo '<h1>Registered Users</h1>';

require_once ('../mysql_pdo_connect.php'); // Connect to the db.

// Make the query:

$sql = "SELECT CONCAT(last_name, ', ', first_name) AS name, DATE_FORMAT(registration_date, '%M %d, %Y') AS dr FROM users ORDER BY registration_date ASC";   

    // Table header.
    echo <<<EOT
    <table align='center' cellspacing='3' cellpadding='3' width='75%'>
    <tr><td align='left'><b>Name</b></td><td align='left'><b>Date Registered</b></td></tr>
EOT;


foreach($DBH->query($sql) as $row){

    $output = "<tr><td align='left'>" . $row["name"] . "</td><td align='left'>" . $row["dr"] . "</td></tr>";


        // echo '<tr><td align="left">' . $row['name'] . '</td><td align="left">' . $row['dr'] . '</td></tr>';
 echo <<<EOT
        $output
EOT;         

        }

    echo '</table>'; // Close the table.
    $DBH = null;


include ('includes/footer.html');
?>

解决方案

You have a whole bunch of spaces (9 to be exact) after the EOT; in the loop.

From the manual

... there may not be any spaces or tabs before or after the semicolon ...

Why are you wrapping the $output variable inside a HEREDOC string? I'd just change the loop to

printf('<tr><td align="left">%s</td><td align="left">%s</td></tr>',
       htmlspecialchars($row["name"]),
       htmlspecialchars($row["dr"]));

or even better, use PHP's alternative syntax.

这篇关于HEREDOC意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆