When do I use the PHP constant “PHP_EOL”?

前端 未结 19 2567
攒了一身酷
攒了一身酷 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:05

    The definition of PHP_EOL is that it gives you the newline character of the operating system you're working on.

    In practice, you should almost never need this. Consider a few cases:

    • When you are outputting to the web, there really isn't any convention except that you should be consistent. Since most servers are Unixy, you'll want to use a "\n" anyway.

    • If you're outputting to a file, PHP_EOL might seem like a good idea. However, you can get a similar effect by having a literal newline inside your file, and this will help you out if you're trying to run some CRLF formatted files on Unix without clobbering existing newlines (as a guy with a dual-boot system, I can say that I prefer the latter behavior)

    PHP_EOL is so ridiculously long that it's really not worth using it.

提交回复
热议问题