Home > Backend Development > PHP Tutorial > The similarities and differences between the file() function and fgets() function in php

The similarities and differences between the file() function and fgets() function in php

怪我咯
Release: 2023-03-13 13:36:02
Original
1418 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template