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

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

提交回复
热议问题