file_put_contents, file_append and line breaks

后端 未结 7 1264
旧时难觅i
旧时难觅i 2020-12-30 19:32

I\'m writing a PHP script that adds numbers into a text file. I want to have one number on every line, like this:

1
5
8
12

If I use f

7条回答
  •  有刺的猬
    2020-12-30 19:45

    pauls answer has the correct approach but he has a mistake. what you need ist the following:

    file_put_contents($filename, trim($commentnumber).PHP_EOL, FILE_APPEND);
    

    the PHP_EOL constant makes sure to use the right line ending on mac, windows and unix systems the trim function removes any newline or whitespace on both sides of the string. converting to integer would be a huge mistake because 1. you might end up having zero, expecially because of white space or special characters (wherever they come from...) 2. ids dont necessarily need to be integers

提交回复
热议问题