The common and different of the file () function and the fgets () function in PHP

Source: Internet
Author: User
Recently write PHP code to read text lines from a text file (text file is the ANSI character set)

PHP 2 built-in functions file and fgets can implement this function

file-to read an entire file into an array

fgets-reading a line from the file pointer

Common denominator: Each text line at the end of both reads contains a newline character (reads text using the ANSI character set, each of which takes one byte, and if the text file is converted to the Utf-8 character set, the result is different, not tested);

Different points: The above is a newline character to \ n input, if the line feed is \ r \ n Input, then file will read into the \r\n,fgets will be processed off \ r reserved \n,\r and \ n with the Ord () function shows the ASCII value is 13 and 10 respectively.

All of these use ANSI character set text tests.

When using fwrite () to write to a text file There is a strange phenomenon, fopen () with ' wt ' Open, write "\ r \ n", with the 16 binary editor to open the text file will find that the line code is CR CR LF (\r\r\n), and the ' W ' mode to open the normal "\ r \ n".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.