Elegant solution for line-breaks (PHP)

后端 未结 8 2027
太阳男子
太阳男子 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:31

    Well, as with any language there are several ways to do it.

    As previous answerers have mentioned, "
    "
    is not a linebreak in the traditional sense, it's an HTML line break. I don't know of a built in PHP constant for this, but you can always define your own:

    // Something like this, but call it whatever you like
    const HTML_LINEBREAK = "
    ";

    If you're outputting a bunch of lines (from an array of strings for example), you can use it this way:

    // Output an array of strings
    $myStrings = Array('Line1','Line2','Line3');
    echo implode(HTML_LINEBREAK,$myStrings);
    

    However, generally speaking I would say avoid hard coding HTML inside your PHP echo/print statements. If you can keep the HTML outside of the code, it makes things much more flexible and maintainable in the long run.

提交回复
热议问题