Print newline in PHP in single quotes

前端 未结 12 1279
野性不改
野性不改 2020-11-28 21:33

I try to use single quotes as much as possible and I\'ve noticed that I can\'t use \\n in single quotes. I know I can just enter a newline literally by pressing return, but

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 22:09

    I wonder why no one added the alternative of using the function chr():

    echo 'Hello World!' . chr(10);
    

    or, more efficient if you're going to repeat it a million times:

    define('C_NewLine', chr(10));
    ...
    echo 'Hello World!' . C_NewLine;
    

    This avoids the silly-looking notation of concatenating a single- and double-quoted string.

提交回复
热议问题