Print newline in PHP in single quotes

前端 未结 12 1202
野性不改
野性不改 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:07
    echo 'hollow world' . PHP_EOL;
    

    Use the constant PHP_EOL then it is OS independent too.

    0 讨论(0)
  • 2020-11-28 22:09

    The only escape sequence you can use in single quotes is for the single quote itself.

    $foo = 'That\'s great';
    

    The only way you could insert a new line into a string created with single quotes is to insert a literal newline

    $bar = 'That\'s
    cheating';
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 22:09

    in case you have a variable :

    $your_var = 'declare your var';
    echo 'i want to show my var here'.$your_var.'<br>';
    
    0 讨论(0)
  • 2020-11-28 22:09

    This worked well for me:

    print_r('Hello world'.PHP_EOL);
    
    0 讨论(0)
  • 2020-11-28 22:16

    No, according to documentation, PHP recognize no special symbol in single quotes. And there is no single reason to use single quotes as much as possible

    0 讨论(0)
提交回复
热议问题