Skip to content
Related Articles

Related Articles

PHP | SplFileObject fgets() Function
  • Last Updated : 19 Dec, 2018
GeeksforGeeks - Summer Carnival Banner

The SplFileObject::fgets() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is usd to get line from file.

Syntax:

string SplFileObject::fgets( void )

Parameters: This function does not accept any parameter.

Return values: This function returns an string containing the next line from the file return FALSE otherwise.

Below Programs illustrate the SplFileObject::fgets() function in PHP.



Program 1:




<?php
  
// Creating SplFile Object
$gfg = new SplFileObject("gfg.txt");
while (!$gfg->eof()) {
    echo $gfg->fgets();
}
?>

Output:

GeeksfoGeeks A Computer Science
Portal 
For Geeks.

Program 2:




<?php
  
// Creating SplFile Object
$gfg = new SplFileObject(__FILE__);
while (!$gfg->eof()) {
    echo $gfg->fgets();
}
?>
Output:
<?php

// Creating SplFile Object
$gfg = new SplFileObject(__FILE__);
while (!$gfg->eof()) {
    echo $gfg->fgets();
}
?>

Reference: http://php.net/manual/en/splfileobject.fgets.php

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :