From the course: PHP Techniques: Working with Files and Directories

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Reading line by line

Reading line by line - PHP Tutorial

From the course: PHP Techniques: Working with Files and Directories

Start my 1-month free trial

Reading line by line

- [Instructor] We know how to read characters and how to read an entire file. Often it's useful to read a file one line at a time. The PHP function fgets returns the next full line from a file. That's one line, all of the texts up until the next line return. It's useful when we want to read a file line by line. Most often you would use it inside a loop. It keeps track of which line is the next line using something called a file pointer which we'll discuss a little later. A classic use would be to read a CSV file where each row contains comma separated values. And then the PHP code can work with each line one at a time. Let's try it. To begin let's switch to fileaccess.php and select all and copy that code again. And then let's open a new file which we will save as file_lines.php and then I'll paste that code in there. I'll also just move this after file read. Instead of working with assetsloremipsum.txt, let's change…

Contents