When do I use the PHP constant “PHP_EOL”?

前端 未结 19 2285
攒了一身酷
攒了一身酷 2020-11-22 06:21

When is it a good idea to use PHP_EOL?

I sometimes see this in code samples of PHP. Does this handle DOS/Mac/Unix endline issues?

相关标签:
19条回答
  • 2020-11-22 07:16

    From main/php.h of PHP version 7.1.1 and version 5.6.30:

    #ifdef PHP_WIN32
    #   include "tsrm_win32.h"
    #   include "win95nt.h"
    #   ifdef PHP_EXPORTS
    #       define PHPAPI __declspec(dllexport)
    #   else
    #       define PHPAPI __declspec(dllimport)
    #   endif
    #   define PHP_DIR_SEPARATOR '\\'
    #   define PHP_EOL "\r\n"
    #else
    #   if defined(__GNUC__) && __GNUC__ >= 4
    #       define PHPAPI __attribute__ ((visibility("default")))
    #   else
    #       define PHPAPI
    #   endif
    #   define THREAD_LS
    #   define PHP_DIR_SEPARATOR '/'
    #   define PHP_EOL "\n"
    #endif
    

    As you can see PHP_EOL can be "\r\n" (on Windows servers) or "\n" (on anything else). On PHP versions prior 5.4.0RC8, there were a third value possible for PHP_EOL: "\r" (on MacOSX servers). It was wrong and has been fixed on 2012-03-01 with bug 61193.

    As others already told you, you can use PHP_EOL in any kind of output (where any of these values are valid - like: HTML, XML, logs...) where you want unified newlines. Keep in mind that it's the server that it's determining the value, not the client. Your Windows visitors will get the value from your Unix server which is inconvenient for them sometimes.

    I just wanted to show the possibles values of PHP_EOL backed by the PHP sources since it hasn't been shown here yet...

    0 讨论(0)
  • 2020-11-22 07:19

    No, PHP_EOL does not handle endline issues, because the system where you use that constant is not the same system where you send the output to.

    I would not recommend using PHP_EOL at all. Unix/Linux use \n, MacOS / OS X changed from \r to \n too and on Windows many applications (especially browsers) can display it correctly too. On Windows, it is also easy change existing client-side code to use \n only and still maintain backward-compatibility: Just change the delimiter for line trimming from \r\n to \n and wrap it in a trim() like function.

    0 讨论(0)
  • 2020-11-22 07:19

    I just experienced this issue when outputting to a Windows client. Sure, PHP_EOL is for server side, but most content output from php is for windows clients. So I have to place my findings here for the next person.

    A) echo 'My Text' . PHP_EOL; // Bad because this just outputs \n and most versions of windows notepad display this on a single line, and most windows accounting software can't import this type of end of line character.

    B) echo 'My Text \r\n'; //Bad because single quoted php strings do not interpret \r\n

    C) echo "My Text \r\n"; // Yay it works! Looks correct in notepad, and works when importing the file to other windows software such as windows accounting and windows manufacturing software.

    0 讨论(0)
  • 2020-11-22 07:20

    I use the PHP_EOL constant in some command line scripts I had to write. I develop on my local Windows machine and then test on a Linux server box. Using the constant meant I didn't have to worry about using the correct line ending for each of the different platforms.

    0 讨论(0)
  • 2020-11-22 07:21

    DOS/Windows standard "newline" is CRLF (= \r\n) and not LFCR (\n\r). If we put the latter, it's likely to produce some unexpected (well, in fact, kind of expected! :D) behaviors.

    Nowadays almost all (well written) programs accept the UNIX standard LF (\n) for newline code, even mail sender daemons (RFC sets CRLF as newline for headers and message body).

    0 讨论(0)
  • 2020-11-22 07:21

    You are writing code that predominantly uses single quote strings.

    echo 'A $variable_literal that I have'.PHP_EOL.'looks better than'.PHP_EOL;  
    echo 'this other $one'."\n";
    
    0 讨论(0)
提交回复
热议问题