Home > Article > Backend Development > The similarities and differences between the file() function and fgets() function in php
Recently written php codeneeds to read text lines from a text file (the text file is ANSI character set)
php’s 2 built-in functions file and fgets can implement this function
file — read the entire file into an array
fgets — read a line from the file pointer
Common ground: Each text line read by both contains a newline character at the end (the two characters each occupy one byte when reading text using the ANSI character set. If the text file is converted into utf-8 characters Set the results are different, not tested);
Differences: The above is that the newline character is input as \n. If the newline character is input as \r\n, then file will read \r\n, and fgets will Dispose of \r and keep \n. The ASCII code values of \r and \n displayed by the ord() function are 13 and 10 respectively.
The above are tested using ANSI character set text.
There is a strange phenomenon when using fwrite() to write to a text file. fopen() is opened with 'wt'. After writing "\r\n", use 16 SystemEditorWhen you open the text file, you will find that the line break code is cr cr lf (\r\r\n), but when you open it in 'w' mode, it is normal "\r\n".
The above is the detailed content of The similarities and differences between the file() function and fgets() function in php. For more information, please follow other related articles on the PHP Chinese website!