How to read a file content line by line using PHP code

Suppose we have file named file.txt we have read a file content line by line using PHP , here our objective is to retrieve a file data line by line using PHP.we know we have most famous php function fopen(“File name”,”r”),it will read entire content from the files and store it in the defined variable
see example

 $f_data = fopen("file.txt","r");
 

Now our question how to read line by line using PHP
Now we know the entire data is their in $f_data,next we have to read line by line using PHP , with the help of while loop we can do that ,for line by line read we use one more function called feof($f_data) which find out the end of the file see full example

 
 $f_data = fopen("file.txt","r");
  
  while(! feof($f_data))  {
	$result = fgets($f_data);
	echo $result."
"; echo "Good"."
"; } fclose($f_data);

e.g
file.txt

Sunday is weekend day
Monday is weekstart day

Out Put

Sunday is weekend day
Good
Monday is weekstart day
Good

Leave a Reply

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