Reputation: 1334
I am trying to replace the HTML code with a heredoc statement. However, I am getting a parse error in the last line.I am sure that I have not left any leading space or indentation on the heredoc closing tag line.Following is a part of the 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 above. As a coding practice, is it better to use PHP code throughout or is it better to use a heredoc syntax. I mean, while in PHP mode, the script bounces back and forth between the HTML and PHP code. So, which is the preferred method?
Upvotes: 3
Views: 2888
Reputation: 1334
Guys, finally I succeeded in getting the parse error out of my way (phew!!). I just rewrote the code and it worked. Here is the code:
$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;
Upvotes: 1
Reputation: 655189
From the PHP manual about the Heredoc syntax:
The closing identifier must begin in the first column of the line.
And a little later in the nice red Warning box:
It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.
So you need to write the code like this to comply with the syntax specification:
$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;
It’s up to you if you really want to use that.
Upvotes: 6
Reputation: 10033
Edit: changed to ob and back and now the loop works for me ...
while($row = mysql_fetch_assoc($result)){
extract($row);
$table .= <<< ENDHTML
<tr>
<td>$showid2</td>
<td>$showname2</td>
</tr>
ENDHTML;
}
Upvotes: 0
Reputation: 31508
Wouldn't this pose a problem?
</tr> ENDHTML;
You should probably:
ENDHTML;
Edit:
Formatting failed ... and Richy C. said the same in the meantime :( I just meant to show that the first ENDHTML;
has whitespace in front of it.
Upvotes: 0
Reputation: 1617
If this code is exactly what you are using, then there are a number of spaces down the left hand side with the exception of the first $table = <<<ENDHTML line. The ending heredoc needs to be exactly left-aligned with no spaces, tabs or other characters to the left of it.
Upvotes: 2
Reputation: 8772
<p><$num_shows Shows</p>
Is that an extraneous '<' character before $num_shows
?
Upvotes: 1