Problem with heredoc instruction

I am trying to replace HTML code with heredoc statement. However, I get a parsing error on the last line. I am sure that I did not leave a single leading space or indentation on the line of the heredoc closing tag. The following is a piece of code:

$table = <<<ENDHTML
    <div style="text-align:center;">
    <table border="0.5" cellpadding="1" cellspacing="1" style="width:50%; margin-left:auto; margin-right:auto;">
    <tr>
    <th>Show I.D</th>
    <th>Show Name</th>
    </tr>
    ENDHTML;
    while($row = mysql_fetch_assoc($result)){
            extract($row);
            $table .= <<<ENDHTML
            <tr>
                <td>$showid2 </td>
                <td>$showname2</td>
            </tr>
    ENDHTML;        
    }
    $table .= <<<ENDHTML
    </table>
    <p><$num_shows Shows</p>
    </div>
    ENDHTML; 
    echo $table;
    ?>

Where is the problem? I have a related question in addition to the above. As a coding practice, is it better to use PHP code in everything or is it better to use heredoc syntax. I mean, in PHP mode, the script bounces back and forth between the HTML and the PHP code. So what is the preferred method?

+3
source share
6 answers

, , (!). , . :

$table = <<<ABC
  <div style="text-align:center;">
  <table border="0.5" cellpadding="1" cellspacing="1" style="width:50%; margin-left:auto; margin-right:auto;">
  <tr>
  <th>Show I.D</th>
  <th>Show Name</th>
  <th>Show Genre</th>
  </tr>
ABC;
  while($row = mysql_fetch_assoc($result))
  {
      extract($row);
$table .= <<<ABC
      <tr>
      <td>$showid2 </td>
      <td>$showname2</td>
      <td>$showtype2_label</td>
      </tr>
ABC;
  }
$table .= <<<ABC
  </table>
  <p>$num_shows Shows</p>
  </div>
ABC;

echo $table;
+1

PHP Heredoc:

.

:

, , (;). , , .

, , :

$table = <<<ENDHTML
    <div style="text-align:center;">
    <table border="0.5" cellpadding="1" cellspacing="1" style="width:50%; margin-left:auto; margin-right:auto;">
    <tr>
    <th>Show I.D</th>
    <th>Show Name</th>
    </tr>
ENDHTML;
    while($row = mysql_fetch_assoc($result)){
                extract($row);
                $table .= <<<ENDHTML
                <tr>
                        <td>$showid2 </td>
                        <td>$showname2</td>
                </tr>
ENDHTML;
    }
    $table .= <<<ENDHTML
    </table>
    <p><$num_shows Shows</p>
    </div>
ENDHTML;
    echo $table;

, .

+6

, , , $table = < < ENDHTML. heredoc , .

+2

<p><$num_shows Shows</p>

'<' $num_shows?

+1

?

</tr>
ENDHTML;

, :

ENDHTML;

Edit: ... Richy C. :( , ENDHTML; .

0

: ob , ...

while($row = mysql_fetch_assoc($result)){
    extract($row);
    $table .= <<< ENDHTML
    <tr>
        <td>$showid2</td>
        <td>$showname2</td>
    </tr>
ENDHTML;
}
0
source

Source: https://habr.com/ru/post/1714232/


All Articles