1

so I'm looking at another person's code trying to fix it, and I'm not sure what is happening. I have a pretty strong knowledge of programming in general, but there is one line that is throwing me off. See below:

<?php
switch ($task) {
    case "createDJ":
          echo <<<END;
          <h5>Create DJ Form</h5>
          <!-- Code for DJ form goes here. -->
          END;
          break;
    case "createShow":
         echo <<<END;
         <h5>Create Show Form</h5>
         <!-- Code for Show form goes here. -->
         END;
         break;
   //...
?>

What is going on with these END statements? I've never seen them before, Also, what is up with the <<< sign?

EDIT: Sorry about the syntax highlighting, not sure why it's all messy.

EDIT: And now I understand why the syntax highlighting is messed up! haha

theangryhornet
  • 403
  • 2
  • 5
  • 14

2 Answers2

6

It's not a statement, it's a way of quoting a string.

It's called called heredoc syntax, and it's supposed to be a convenient way to quote a multi-line string. <<<END starts it and END at the beginning of a line ends it. (END is the programmer's choice, they can use an identifier they want.)

This is explained here in the PHP documentation:

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Jeremy
  • 1
  • 85
  • 340
  • 366
  • Relevant documentation: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc. – rovaughn Sep 17 '11 at 20:45
  • 2
    Worth to note: It's called "heredoc". You can create multiline-strings with _any_ string-syntax – KingCrunch Sep 17 '11 at 20:45
1

It's the heredoc syntax for strings, available in a couple of languages.

<<<HERE
string text here
HERE
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176