So. Im sat here reading a php book and it says you can use <<<EOF at the beginning of your echo to sort of.. temporarily switch back to HTML mode, thus dispersing with the need for escaping characters. So I try it...

<?php
echo <<<EOF
<a href="index.php">Home</a>
EOF;
?>

Just like the book says, and I get

Parse error: parse error, unexpected $ in (my path) on line 5

Did my book lie? Or am I doing something dumb?

    You sure there isn't any trailing whitespace on the

    EOF;

    line? Or for that matter on the

    echo <<<EOF

    line?

    But your book does sound like it's oversimplifying though; heredoc quotes (as the things are called) are more like custom quote marks; the result is just string like any ordinary (double-quoted) string in PHP, and behaves as such. The benefit with them is that it means that you can specify exactly what label will end the string (EOF in the case of this example). Since there's only one thing that can end the string (EOF; on a line by itself), the only thing you have to make sure of is that the string you use for your label doesn't appear a line by itself inside the string.

    If you want to "temporarily switch back to HTML mode" then that's

    <?php
    ....
    ?>
    <a href="index.php">Home</a> 
    <?php
    ....
    ?>
    

      Yes Im sure theres no whitespace, I read on the net somewhere to check for that.
      Hmm.. yea Im familiar with that method of switching back into HTML of course, the book said the advantage of heredoc quotes was that you could use php variables within your HTML and theyd still work. It seemed simpler to me than constantly doing <?php echo ($var); ?> where I have blocks of HTML that contain several php vars.

        I copied then pasted your code from your post and ran it and it worked just fine. There can be no space after the <<<EOF and there can be no space before or after the EOF;

          It seemed simpler to me than constantly doing <?php echo ($var); ?> where I have blocks of HTML that contain several php vars.

          True; just remember that the only difference between

          echo <<<EOF
          <a href="index.php">Home with a $variable</a>
          <a href="index.php">Home with a $variable</a>
          <a href="index.php">Home with a $variable</a>
          EOF;
          

          and

          echo "<a href=\"index.php\">Home with a $variable</a>
          <a href=\"index.php\">Home with a $variable</a>
          <a href=\"index.php\">Home with a $variable</a>";

          is when you have " characters to escape. "switching in and out of HTML" isn't really an appropriate way of looking at it. For example

          $text = <<<EOF
          <a href="index.php">Home with a $variable</a>
          <a href="index.php">Home with a $variable</a>
          <a href="index.php">Home with a $variable</a>
          EOF;
          

          will do the same thing as

          $text = "<a href=\"index.php\">Home with a $variable</a>
          <a href=\"index.php\">Home with a $variable</a>
          <a href=\"index.php\">Home with a $variable</a>";
          

          But

          $text = ?>
          <a href="index.php">Home with a <?php echo $variable?></a>
          <a href="index.php">Home with a <?php echo $variable?></a>
          <a href="index.php">Home with a <?php echo $variable?></a>
          <?php
          

          won't.

            nods in context though Im using this only for echos, so presumably

            php stuff... ?>
            <a href="whatever">whatever</a>
            <a href="<?php echo $var; ?>"><?php echo $var; ?></a>
            <?php 
            

            Does do the same thing as echoing it out with escaped characters? It seems to work ok anyway. After thinking about it briefly I decided that this methods superior to EOF when writing in dreamweaver, because "jumping" back into HTML mode for long HTML bits means that everything appears in the right colors!

              Write a Reply...