PHP fgets, fgetc and feof functions

PHP fgets(), fgetc() and feof() file handling

In this php tutorial we will learn how use fgets() function, fgetc() function and feof() function with php example.

PHP File handling functions :

  • fgets() – used to read a single line from a file.
  • fgetc() – used to read a single character from a file.
  • feof() – used to check ‘end of file’.

PHP fgets() Function

The PHP fgets() function is used to read a single line from a file.

fgets() function syntax

<?php

fgets(“filename”);

?>

PHP fgets() function example

The fgets() function read a single first line from a file. For read a file we must first open file using fopen() function and the read a line using fgets() function after using fgets() function we have to close the opened file using fclose() function. Here, we open a ‘abc.txt’ file in read mode(‘r’) and read a line using fgets() function in below php example.

The out put is : Read only first single line from a file. “Hello Meera Academy”. Now, lets take a php form example for fgets() function. First we create a new file with some text contents and after we read file using fgets() function the output will be the first line from a file.

PHP fgets() function example

The out put is :

PHP fgets function example

PHP fgets() function example


 

PHP fgetc() Function

The php fgetc() function is used to read a single character from a file.

fgetc() function syntax

<?php

fgetc(“filename”);

?>

fgetc() function example

The fgetc() function rad a single first character from a file. Here, we open a ‘abc.txt’ file in read mode(‘r’) and read a character using fgetc() function in below php example.

The out put is : “M”. Read only first character from a file.

PHP form example for fgetc() function.

The output is:

PHP fgetc function example

PHP fgetc() function example


PHP feof() Function

The feof() function is used to check whether the file has been reached to end or not. This function used in looping for get all line from file one by one to check the end of file reached or not.

feof() function syntax

<?php

feof(“filename”);

?>

php feof example :

 

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 8 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">