Best Practices: working with long, multiline strings in PHP?

前端 未结 10 2058
感情败类
感情败类 2020-11-27 09:59

Note: I\'m sorry if this is an extremely simple question but I\'m somewhat obsessive compulsive over the formatting of my code.

I have a class that has a function th

相关标签:
10条回答
  • 2020-11-27 10:33

    I use similar system as pix0r and I think that makes the code quite readable. Sometimes I would actually go as far as separating the line breaks in double quotes and use single quotes for the rest of the string. That way they stand out from the rest of the text and variables also stand out better if you use concatenation rather than inject them inside double quoted string. So I might do something like this with your original example:

    $text = 'Hello ' . $vars->name . ','
          . "\r\n\r\n"
          . 'The second line starts two lines below.'
          . "\r\n\r\n"
          . 'I also don\'t want any spaces before the new line,'
          . ' so it\'s butted up against the left side of the screen.';
    
    return $text;
    

    Regarding the line breaks, with email you should always use \r\n. PHP_EOL is for files that are meant to be used in the same operating system that php is running on.

    0 讨论(0)
  • 2020-11-27 10:35

    I like this method a little more for Javascript but it seems worth including here because it has not been mentioned yet.

    $var = "pizza";
    
    $text = implode(" ", [
      "I love me some",
      "really large",
      $var,
      "pies.",
    ]);
    
    // "I love me some really large pizza pies."
    

    For smaller things, I find it is often easier to work with array structures compared to concatenated strings.

    Related: implode vs concat performance

    0 讨论(0)
  • 2020-11-27 10:35

    The one who believes that

    "abc\n" . "def\n"
    

    is multiline string is wrong. That's two strings with concatenation operator, not a multiline string. Such concatenated strings cannot be used as keys of pre-defined arrays, for example. Unfortunately php does not offer real multiline strings in form of

    "abc\n"
    "def\n"
    

    only HEREDOC and NOWDOC syntax, which is more suitable for templates, because nested code indent is broken by such syntax.

    0 讨论(0)
  • 2020-11-27 10:41

    Adding \n and/or \r in the middle of the string, and having a very long line of code, like in second example, doesn't feel right : when you read the code, you don't see the result, and you have to scroll.

    In this kind of situations, I always use Heredoc (Or Nowdoc, if using PHP >= 5.3) : easy to write, easy to read, no need for super-long lines, ...

    For instance :

    $var = 'World';
    $str = <<<MARKER
    this is a very
    long string that
    doesn't require
    horizontal scrolling, 
    and interpolates variables :
    Hello, $var!
    MARKER;
    

    Just one thing : the end marker (and the ';' after it) must be the only thing on its line : no space/tab before or after !

    0 讨论(0)
  • 2020-11-27 10:42

    In regards to your question about newlines and carriage returns:

    I would recommend using the predefined global constant PHP_EOL as it will solve any cross-platform compatibility issues.

    This question has been raised on SO beforehand and you can find out more information by reading "When do I use the PHP constant PHP_EOL"

    0 讨论(0)
  • 2020-11-27 10:46

    you can also use:

    <?php
    ob_start();
    echo "some text";
    echo "\n";
    
    // you can also use: 
    ?> 
    some text can be also written here, or maybe HTML:
    <div>whatever<\div>
    <?php
    echo "you can basically write whatever you want";
    // and then: 
    $long_text = ob_get_clean();
    
    
    0 讨论(0)
提交回复
热议问题