Elegant solution for line-breaks (PHP)

后端 未结 8 2009
太阳男子
太阳男子 2020-12-08 14:28
$var = \"Hi there\".\"
\".\"Welcome to my website\".\"
;\" echo $var;

Is there an elegant way to handle line-breaks in PHP? I\'

8条回答
  •  失恋的感觉
    2020-12-08 15:14

    For linebreaks, PHP as "\n" (see double quote strings) and PHP_EOL.

    Here, you are using
    , which is not a PHP line-break : it's an HTML linebreak.


    Here, you can simplify what you posted (with HTML linebreaks) : no need for the strings concatenations : you can put everything in just one string, like this :

    $var = "Hi there
    Welcome to my website
    ";

    Or, using PHP linebreaks :

    $var = "Hi there\nWelcome to my website\n";
    

    Note : you might also want to take a look at the nl2br() function, which inserts
    before \n.

提交回复
热议问题