开发者

'END' keyword in switch case?

开发者 https://www.devze.com 2023-04-05 09:16 出处:网络
so I\'m looking at another per开发者_StackOverflow社区son\'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 lin

so I'm looking at another per开发者_StackOverflow社区son'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


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.


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

<<<HERE
string text here
HERE
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号