When do I use the PHP constant “PHP_EOL”?

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

    PHP_EOL (string) The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2

    You can use this constant when you read or write text files on the server's filesystem.

    Line endings do not matter in most cases as most software are capable of handling text files regardless of their origin. You ought to be consistent with your code.

    If line endings matter, explicitly specify the line endings instead of using the constant. For example:

    • HTTP headers must be separated by \r\n
    • CSV files should use \r\n as row separator
    0 讨论(0)
  • 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.

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

    Yes, PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.

    Note that PHP_EOL represents the endline character for the current system. For instance, it will not find a Windows endline when executed on a unix-like system.

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

    I found PHP_EOL very useful for file handling, specially if you are writing multiple lines of content into a file.

    For example, you have a long string that you want to break into the multiple lines while writing into plain file. Using \r\n might not work so simply put PHP_EOL into your script and the result is awesome.

    Check out this simple example below:

    <?php
    
    $output = 'This is line 1' . PHP_EOL .
              'This is line 2' . PHP_EOL .
              'This is line 3';
    
    $file = "filename.txt";
    
    if (is_writable($file)) {
        // In our example we're opening $file in append mode.
        // The file pointer is at the bottom of the file hence
        // that's where $output will go when we fwrite() it.
        if (!$handle = fopen($file, 'a')) {
             echo "Cannot open file ($file)";
             exit;
        }
        // Write $output to our opened file.
        if (fwrite($handle, $output) === FALSE) {
            echo "Cannot write to file ($file)";
            exit;
        }
        echo "Success, content ($output) wrote to file ($file)";
        fclose($handle);
    } else {
        echo "The file $file is not writable";
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 07:12

    Handy with error_log() if you're outputting multiple lines.

    I've found a lot of debug statements look weird on my windows install since the developers have assumed unix endings when breaking up strings.

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

    There is one obvious place where it might be useful: when you are writing code that predominantly uses single quote strings. Its arguable as to whether:

    echo 'A $variable_literal that I have'.PHP_EOL.'looks better than'.PHP_EOL;  
    echo 'this other $one'."\n";
    

    The art of it is to be consistent. The problem with mix and matching '' and "" is that when you get long strings, you don't really want to have to go hunting for what type of quote you used.

    As with all things in life, it depends on the context.

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