PHP fgets() ຫົວຫນື່ງ

ການອະທິບາຍ ແລະ ການນຳໃຊ້

ຫົວຫນື່ງ fgets() ຈະອົດຈອນປະເພດດຽວທີ່ຈະບັນຈຸຈາກຄຳນວຍສາຍເຫລົ່າ.

ການນຳໃຊ້

fgets(file,length)
ປະເພດ ກ່າວຄວາມ
file ສະບັບ. ກໍານົດບັນຊີທີ່ຈະອົດຈອນ.
length ຄວນເລືອກ. ກໍານົດຈຳນວນຕົວເລກທີ່ຈະອົດຈອນ. ຈຳນວນມັດຕາຍເປັນ 1024 ຕົວເລກ.

Description

from file Reads a line from the file pointed to and returns the length up to length - 1-byte string. Encountered newline (including in the return value), EOF, or after reading length - Stop after 1 byte (depending on which one is encountered first). If not specified length, then it defaults to 1K, or 1024 bytes.

If it fails, it returns false.

Tips and Comments

Note:length The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3 onwards, ignoring length It will continue to read data from the stream until the end of the line. If most of the lines in the file are greater than 8 KB, specifying the maximum line length in the script is more effective in terms of resource usage.

Note:This function can be used safely with binary files from PHP 4.3 onwards. Early versions do not.

Note:If PHP cannot recognize the line ending of Macintosh files when reading the file, you can enable the auto_detect_line_endings runtime configuration option.

Example

Example 1

<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>

Output similar to:

Hello, this is a test file.

Example 2

<?php
$file = fopen("test.txt","r");
while(! feof($file))
  {
  echo fgets($file). "<br />";
  }
fclose($file);
?>

Output similar to:

Hello, this is a test file. 
ມີສາມປະເດັນຢູ່ທີ່ນີ້. 
ນີ້ແມ່ນປະເດັນສຸດທ້າຍ.