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

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

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

提交回复
热议问题