Hello Daniweb posters. I have a code that is supposed to make a sign up sheet for a web page. Problem is, the code does not work. When I run the code it gives me:
syntax error, unexpected T_PRINT in line 15.
I know there is something wrong with the heredoc syntax I have, but I do not know why.

<?php
 if ($_POST["page"] == "confirm")
 {
   confirmPage ();
 }
 else
 {
    orderForm();
 }

 function orderForm()
 {
  $script = $_SERVER['PHP_SELF'];
    print <<<TOP
  <html>
  <head>
  <title> Online Sign Up </title>
  </head>
  <body>
  <h3> Online Sign Up</h3>
  <form method = "post" action = "$script">
    <p>
    <table border = "2" width="400">
    <th>Time</th>
    <th>Name</th>

TOP;
    $schedule = getSchedule();
    for($i = 0; $i<count($schedule); $i+=2)
    {
        $index = $i+1;
        print("<tr>");
        print("<td> <center> $schedule[$i] </center> </td>");
        if($schedule[$index]=="")
        {
            print("<td> <center> <input type = \"text\" name = \"$index\" size = \"30\"> </center> </td>");
        }
        else
        {
            print("<td> <center> $schedule[$index] </center> </td>");
        }
        print("</tr>");
    }
  print <<<BOTTOM
  </table>
  <input type = "hidden" name = "page" value = "confirm" />
  </p>
  <p>
  <input type = "submit" value = "Submit" />
  </p>
  </form>
  </body>
  </html>
BOTTOM;
  }

  function getSchedule()
  {
    $lines = file("./signup.txt", FILE_IGNORE_NEW_LINES);
  $schedule = array();
  foreach ($lines as $line_num => $line)
  {
    $schedule[$line_num] = $line;
  }
  return $schedule;
  }

  function setSchedule($schedule)
  {
    $file = fopen ("./signup.txt", w);
    for($i=0; $i<count($schedule); $i++)
    {
        fwrite($file, $schedule[$i]."\n");
    }
    fclose($file);
  }

  function confirmPage()
  {
    $script = $_SERVER['PHP_SELF'];
    $schedule = getSchedule();
    print <<<TOP
    <html>
    <head>
    <title> Sign Up </title>
    </head>
    <body>
    <h2> Confirmation </h2>
    <p>
TOP;
    for($i=1; $i<=count($schedule); $i+=2)
    {
        if($_POST[$i])
        {
            if($schedule[$i] == "")
            {
                $index = $i-1;
                $schedule[$i] = $_POST[$i];
                print("$schedule[$i] is now scheduled for $schedule[$index] \n");
            }
            else
            {
                print <<<ERROR
                Unable to add $schedule[$i] to $schedule[$index]. Please enter another time.
                <form>
                <input type = "hidden" name = "page" value = "" />
                <input type = "submit" value = "Return" />
                </form>
ERROR;
            }
        }
    }
    setSchedule($schedule);

    print <<<BOTTOM
    </p>
    </body>
    </html>
BOTTOM;
  }
?>

Dani AI

Generated

The "syntax error, unexpected T_PRINT" at the line you quoted usually means a heredoc was never properly terminated, so the parser reached the next print and choked. Heredoc rules to remember: the opening <<<IDENT must be followed by a newline, and the closing identifier (the same IDENT) must appear alone on its own line, starting in column 1 with no spaces or tabs and immediately followed by a semicolon. Indenting the closing marker is the most common cause of this exact error. 's tip to use echo works, and 's approach of switching between PHP and HTML is fine, but fixing the heredoc is straightforward and keeps the code compact.

Example of correct heredoc usage (note the closing marker has no leading whitespace):

<?php
$script = $_SERVER['PHP_SELF'];
echo <<<HTML
<form method="post" action="$script">
  <input type="hidden" name="page" value="confirm" />
</form>
HTML;
?>

Additional issues not yet called out in the thread: fopen("./signup.txt", w) must use quotes for the mode ('w' or "w"); loops using for($i=1; $i<=count($schedule); ...) risk out-of-bounds access—use < count(...); when embedding array values in double-quoted strings prefer braces ("{$schedule[$i]}") to avoid ambiguity; check file() return value before iterating; and sanitize output with htmlspecialchars() to avoid XSS. When writing the file, use file_put_contents(..., LOCK_EX) or flock() to avoid race conditions.

Quick debugging tips: run php -l yourfile.php to lint for syntax errors, enable error_reporting(E_ALL) and ini_set('display_errors', 1) while developing, then fix one issue at a time and retest. This will pinpoint the heredoc problem fast for .

Recommended Answers

All 4 Replies

Can you show the full, unmarked code please?

From the looks of it your code is marked to tell us where the errors are, but that does not help us in terms of what TOP and BOTTOM mean.

If this is your actual code, I suggest you look heavily into PHP tutorials.

Use the PHP delimiters before starting HTML tags.Change your code as to

<?php
 if ($_POST["page"] == "confirm")
 {
   confirmPage ();
 }
 else
 {
    orderForm();
 }

 function orderForm()
 {
  $script = $_SERVER['PHP_SELF'];
    ?>
	  <html>
  <head>
  <title> Online Sign Up </title>
  </head>
  <body>
  <h3> Online Sign Up</h3>
  <form method = "post" action = "$script">
    <p>
    <table border = "2" width="400">
    <th>Time</th>
    <th>Name</th>

<?php
    $schedule = getSchedule();
    for($i = 0; $i<count($schedule); $i+=2)
    {
        $index = $i+1;
        print("<tr>");
        print("<td> <center> $schedule[$i] </center> </td>");
        if($schedule[$index]=="")
        {
            print("<td> <center> <input type = \"text\" name = \"$index\" size = \"30\"> </center> </td>");
        }
        else
        {
            print("<td> <center> $schedule[$index] </center> </td>");
        }
        print("</tr>");
    }
 ?>
  </table>
  <input type = "hidden" name = "page" value = "confirm" />
  </p>
  <p>
  <input type = "submit" value = "Submit" />
  </p>
  </form>
  </body>
  </html>
<?php
  }

  function getSchedule()
  {
    $lines = file("./signup.txt", FILE_IGNORE_NEW_LINES);
  $schedule = array();
  foreach ($lines as $line_num => $line)
  {
    $schedule[$line_num] = $line;
  }
  return $schedule;
  }

  function setSchedule($schedule)
  {
    $file = fopen ("./signup.txt", w);
    for($i=0; $i<count($schedule); $i++)
    {
        fwrite($file, $schedule[$i]."\n");
    }
    fclose($file);
  }

  function confirmPage()
  {
    $script = $_SERVER['PHP_SELF'];
    $schedule = getSchedule();
   ?>
    <html>
    <head>
    <title> Sign Up </title>
    </head>
    <body>
    <h2> Confirmation </h2>
    <p>
<?php
    for($i=1; $i<=count($schedule); $i+=2)
    {
        if($_POST[$i])
        {
            if($schedule[$i] == "")
            {
                $index = $i-1;
                $schedule[$i] = $_POST[$i];
                print("$schedule[$i] is now scheduled for $schedule[$index] \n");
            }
            else
            {
               
               echo " Unable to add $schedule[$i] to $schedule[$index]. Please enter another time.";
			   ?>
                <form>
                <input type = "hidden" name = "page" value = "" />
                <input type = "submit" value = "Return" />
                </form>
<?php
            }
        }
    }
    setSchedule($schedule);

  ?>
    </p>
    </body>
    </html>
<?php
  }
?>

remove the

print()

and change it into

echo()

in line 15.

print <<<TOP
<html> ... </html>

to

echo "
<html>... </html>";

Okay, I will do the above decade.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.