PHP | fgetc( ) Function
The fgetc() function in PHP is an inbuilt function which is used to return a single character from an open file. It is used to get a character from a given file pointer.
The file to be checked is used as a parameter to the fgetc() function and it returns a string containing a single character from the file which is used as a parameter.
Syntax:
fgetc($file)
Parameters: The fgetc() function in PHP accepts only one parameter $file. It specifies the file from which character is needed to be extracted.
Return Value: It returns a string containing a single character from the file which is used as a parameter.
Errors And Exception:
- The function is not optimised for large files since it reads a single character at a time and it may take a lot of time to completely read a long file.
- The buffer must be cleared if the fgetc() function is used multiple times.
- The fgetc() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.
Below programs illustrate the fgetc() function.
Program 1: In the below program the file named gfg.txt contains the following text.
This is the first line.
This is the second line.
This is the third line.
<?php // file is opened using fopen() function $my_file = fopen("gfg.txt", "rw"); // Prints a single character from the // opened file pointer echo fgetc($my_file); // file is closed using fclose() function fclose($my_file); ?> |
Output:
T
Program 2: In the below program the file named gfg.txt contains the following text.
This is the first line.
This is the second line.
This is the third line.
<?php // file is opened using fopen() function $my_file = fopen("gfg.txt", "rw"); // prints single character at a time // until end of file is reached while (! feof ($my_file)) { echo fgetc($my_file); } // file is closed using fclose() function fclose($my_file); ?> |
Output:
This is the first line. This is the second line. This is the third line.
Reference:
http://php.net/manual/en/function.fgetc.php
Recommended Posts:
- PHP | SplFileObject fgetc() Function
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | pi( ) Function
- PHP | Ds\Set xor() Function
- PHP | Ds\Map xor() Function
- PHP | Ds\Set add() Function
- PHP | pow( ) Function
- PHP | each() Function
- PHP | Ds\Map put() Function
- p5.js | hex() function
- PHP | key() Function
- CSS | rgb() Function
- PHP | min( ) Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



