2
if(...) {
    if(...) {
        echo <<<EOT
        xxxxxx
EOT;
    } 
}

or

if(...) {
    if(...) {
        echo <<<EOT
        xxxxx
        EOT;
    } 
}

I prefer the latter, because it won't break the structure of code.

But it seems that the latter will cause highlight error in most editors.

Are there any standard stipulate that we must write as the former one? If so, why they make such a unsatisfying standard?

And are there any alternatives for writing block strings in php that won't break the code structure as the former one does? It is really unsatisfying...

Lixin Wei
  • 441
  • 5
  • 17
  • 1
    It's called HEREDOC syntax and yes, the identifier **must** be at the beginning of the line : http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – CD001 Mar 30 '17 at 08:13
  • @CD001 Okay...but are there any alternatives for writing block strings in php that won't break the code structure the HEREDOC does? It is really unsatisfying... – Lixin Wei Mar 30 '17 at 08:15
  • Why not just visit the link I put in to PHP string types? Gives you all the possible options. – CD001 Mar 30 '17 at 08:16
  • @Lixin Wei How about using it with `include`, and in the external file: `return << – JustOnUnderMillions Mar 30 '17 at 08:17
  • ... or just break out of PHP `if(something): ?> ... content here ... – CD001 Mar 30 '17 at 08:18
  • @CD001 thanks, I'll take care. – Lixin Wei Mar 30 '17 at 08:26

3 Answers3

2

Edit for PHP version 7.3:

Beginning in PHP version 7.3 the Heredoc and Nowdoc syntax has been made flexible, so the code in OP is now valid as is. The indentation of the closing statement determines how much whitespace is stripped from the string.

Official release statement with more info: https://www.php.net/manual/en/migration73.new-features.php#migration73.new-features.core.heredoc

Original answer valid for PHP versions <= 7.2:

As others have pointed out, the heredoc syntax is strict and the closing statement must be placed on its own line with no whitespace.

There are very legit reasons for using the heredoc, see this SO-question for example: What is the advantage of using Heredoc in PHP?
If that is the case for you, then moving the block string to an external file as other answers suggest might be the best option if you want to keep your indentation consistent.

Alternatives to heredoc

If single or double quoted strings contain text with line breaks, all the whitespace from the indentation is preserved in the output, but this is suppressed in browsers.

Depending on what you want with your output, you could write your own wrapper function, for example if you want to show the newlines in the browser output:

if( true ) {
  if( true ) {

    // Single quoted
    $single = 'some
    text';
    echo $single; // some text

    // Double quoted
    $double = "some
    text";
    echo $double; // some text

    // Custom function
    function block($s) {
      return preg_replace('/\s*\n\s*/', '<br/>', $s);
    }
    echo block("some
    text"); // some<br/>text
    // In a browser this is shown as:
    // some
    // text
  }
}

This last example with the block function strips all the whitespace around the newline character (including the newline) and replaces it with a <br/> tag.

glaux
  • 701
  • 7
  • 20
1

Direct from PHP:

Warning It is very important to note that the line with the closing identifier must contain no other characters, except 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.

PHP Manual's reference

PhilS
  • 365
  • 2
  • 8
1

You can move it to an external file like:

return <<<"EOF"
  Hello $var
EOF;

then get it via include

if(...) {
  if(...) {
    $var = 'World';
    print include 'file.php';
  } 
}

result is

Hello World

JustOnUnderMillions
  • 3,741
  • 9
  • 12