Skip to content
Related Articles

Related Articles

PHP | SplFileObject fgetc() Function
  • Last Updated : 19 Dec, 2018

The SplFileObject::fgetc() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to get character from file.
Syntax:

string SplFileObject::fgetc( void )

Parameters: This function does not accept any parameter.

Return values: Returns single character read from the file or FALSE on EOF.

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

Note: Program 1 has used gfg.txt file that contains following data.



GeeksforGeeks

Program 1: Print All characters of file by one space separated.




<?php
    
// Create SplFileObject object
$file = new SplFileObject("gfg.txt");
    
// Print all characters of file 
while (false !== ($gfg = $file->fgetc()))
    
{
    echo "$gfg";
}
?>

Output:

G e e k s f o r G e e k s

Program 2: Print all characters of current file.




<?php
   
// Create SplFileObject object
$file = new SplFileObject(__FILE__);
   
// Print all characters of file 
while (false !== ($gfg = $file->fgetc()))
   
{
    echo "$gfg";
}
?>
Output:
<?php
 
// Create SplFileObject object
$file = new SplFileObject(__FILE__);
 
// Print all characters of file 
while (false !== ($gfg = $file->fgetc()))
 
{
    echo "$gfg";
}
?>

Reference: http://php.net/manual/en/splfileobject.fgetc.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 :