
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to read only 5 last line of the text file in PHP?
To read only 5 last lines of the text file, the code is as follows −
Example
$file = file("filename.txt"); for ($i = max(0, count($file)-6); $i < count($file); $i++) { echo $file[$i] . "
"; }
Output
This will produce the following output −
Given that the file has more than 5 lines of text, the last 5 lines of the text file will be displayed.
The file is opened and the number of lines in the file are counted, and beginning from the last line, 5 lines are read.
- Related Articles
- Read last line from file in PHP
- How to read complete text file line by line using Python?
- How to read only the first line of a file with Python?
- How to read an entire line from a text file using Python?
- What are some of the fastest way to read a text file line by line using C#?
- How to read a text file in Python?
- How to make the Tkinter text widget read only?
- How to read a text file with C++?
- Golang program to read the content of a file line by line
- How to read a simple text file in Android App?
- How to read a text file in Selenium with python?
- How to read a text file from resources in Kotlin?
- How to make Laravel (Blade) text field read-only?
- Read file line by line using C++
- How to read a Specific Line From a File in Linux?

Advertisements