
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
fgets() function in PHP
The fgets() function returns a line from a file. It returns a string of up to length - 1 bytes read from the file pointed to by file_pointer.
Syntax
fgets (file_pointer, length);
Parameters
file_pointer − The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen().
length − Reading ends when length - 1 bytes have been read, on a newline, or on EOF (whichever comes first).
Return
The fgets() function returns a string of up to length - 1 bytes read from the file pointed to by fle_pointer.
Example
The following is an example. Here, we have a file “one.txt” with text “This is it!”.
<?php $file_pointer = @fopen("/new/one.txt", "w"); if ($file_pointer) { while (!feof($file_pointer)) { $buffer = fgets($file_pointer, 512); echo $buffer; } fclose($file_pointer); } ?>
Output
This is it!
Let us see another example.
Example
Here, we have a text file “new.txt”, with the text, “This is demo text”.
<?php $file_pointer = fopen("new.txt","r"); $res = fgets($file_pointer); echo $res; fclose($file_pointer); ?>
Output
This is demo text
- Related Articles
- fgets() and fread() - What is the difference in PHP?
- fgets() and gets() in C
- filter_has_var() function in PHP
- filter_id() function in PHP
- filter_input() function in PHP
- filter_input_array() function in PHP
- filter_list() function in PHP
- filter_var_array() function in PHP
- filter_var() function in PHP
- constant() function in PHP
- define() function in PHP
- defined() function in PHP
- die() function in PHP
- eval() function in PHP
- exit() function in PHP

Advertisements