views:

185

answers:

3

I get this error message

Parse error: syntax error, unexpected $end in E:\xampp\htdocs\announcements\announcement.php on line 143

Line 143 is the last line of the PHP file. When I comment out

$htmlcode=<<<eod
<div>$question</div>
<div>$option1  $option2  $option3  $option4</div><br/>
eod;    
echo $htmlcode;

The error is gone. What's wrong?

+2  A: 

You have spaces after eod;

As stated in the manual

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. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) must also be followed by a newline.

Peter Lindqvist
+1 for figuring out those nasty whitespaces
Atmocreations
A: 

Enclose the variable names inside your heredoc block with { and } like so:

$htmlcode=<<<eod
<div>{$question}</div>
<div>{$option1}  {$option2}  {$option3}  {$option4}</div><br/>
eod;
echo $htmlcode;

The problem is that PHP chokes on the fact that you have no whitespace separating your $question and $option4 variables from the opening < for your closing div tags.

Also, ensure that there's no whitespace after the semicolon following your eod delimiter. The only thing allowed on that line is your delimiter and a semicolon if necessary.

Adam Maras
Actually, that's not really the case. Have you tried the code?
Peter Lindqvist
Ehm, i mean about the variables. The part about whitespace is true.
Peter Lindqvist
Not directly. But personal experience with PHP has led me down the path of debugging problems like this for hours upon hours.
Adam Maras
Perhaps it's not such bad advice after all.
Peter Lindqvist
+2  A: 

What I found out what after your eod;, you had some whitespaces there.

Remove the whitespaces and it will work fine.

Tested:

<?php
$htmlcode=<<<eod
<div>$question</div>
<div>$option1  $option2  $option3  $option4</div><br/>
eod;
echo $htmlcode;
?>
thephpdeveloper
Cool.Right on target! Genius.
Steven
nothing much, just ran the code and tried out different ways to see what's the problem.
thephpdeveloper