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

前端 未结 10 2063
感情败类
感情败类 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:47

    but what's the deal with new lines and carriage returns? What's the difference? Is \n\n the equivalent of \r\r or \n\r? Which should I use when I'm creating a line gap between lines?

    No one here seemed to actualy answer this question, so here I am.

    \r represents 'carriage-return'

    \n represents 'line-feed'

    The actual reason for them goes back to typewriters. As you typed the 'carriage' would slowly slide, character by character, to the right of the typewriter. When you got to the end of the line you would return the carriage and then go to a new line. To go to the new line, you would flip a lever which fed the lines to the type writer. Thus these actions, combined, were called carriage return line feed. So quite literally:

    A line feed,\n, means moving to the next line.

    A carriage return, \r, means moving the cursor to the beginning of the line.

    Ultimately Hello\n\nWorld should result in the following output on the screen:

    Hello
    
         World
    

    Where as Hello\r\rWorld should result in the following output.

    It's only when combining the 2 characters \r\n that you have the common understanding of knew line. I.E. Hello\r\nWorld should result in:

    Hello
    World
    

    And of course \n\r would result in the same visual output as \r\n.

    Originally computers took \r and \n quite literally. However these days the support for carriage return is sparse. Usually on every system you can get away with using \n on its own. It never depends on the OS, but it does depend on what you're viewing the output in.

    Still I'd always advise using \r\n wherever you can!

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

    I use templates for long text:

    email-template.txt contains

    hello {name}!
    how are you? 
    

    In PHP I do this:

    $email = file_get_contents('email-template.txt');
    $email = str_replace('{name},', 'Simon', $email);
    
    0 讨论(0)
  • 2020-11-27 10:50

    You should use HEREDOC or NOWDOC.

    $var = "some text";
    $text = <<<EOT
      Place your text between the EOT. It's
      the delimiter that ends the text
      of your multiline string.
      $var
    EOT;
    

    The difference between Heredoc and Nowdoc is that PHP code embedded in a heredoc gets executed, while PHP code in Nowdoc will be printed out as is.

    $var = "foo";
    $text = <<<'EOT'
      My $var
    EOT;
    

    In this case $text will have the value My $var.

    Note: before the closing EOT; there should be no spaces or tabs. otherwise you will get an error

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

    Sure, you could use HEREDOC, but as far as code readability goes it's not really any better than the first example, wrapping the string across multiple lines.

    If you really want your multi-line string to look good and flow well with your code, I'd recommend concatenating strings together as such:

    $text = "Hello, {$vars->name},\r\n\r\n"
        . "The second line starts two lines below.\r\n"
        . ".. Third line... etc";
    

    This might be slightly slower than HEREDOC or a multi-line string, but it will flow well with your code's indentation and make it easier to read.

    0 讨论(0)
提交回复
热议问题