Reading files (fread, fgets, file) in PHP - Time & Space Complexity
When reading files in PHP, it's important to know how the time to read grows as the file gets bigger.
We want to understand how the reading operations scale with file size.
Analyze the time complexity of the following code snippet.
$handle = fopen('example.txt', 'r');
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
This code reads a file line by line and prints each line until the end of the file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line from the file using fgets inside a loop.
- How many times: Once for every line in the file, repeating until the file ends.
As the file gets bigger, the number of lines grows, so the loop runs more times.
| Input Size (lines) | Approx. Operations (reads) |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The number of read operations grows directly with the number of lines in the file.
Time Complexity: O(n)
This means the time to read the file grows in a straight line with the number of lines in the file.
[X] Wrong: "Reading a file line by line is always very fast and constant time."
[OK] Correct: The time depends on how many lines the file has; more lines mean more reading steps, so it grows with file size.
Understanding how file reading scales helps you write efficient code and explain your choices clearly in real projects or interviews.
"What if we used fread to read the whole file at once instead of fgets line by line? How would the time complexity change?"
Practice
Solution
Step 1: Understand function behavior
fgets()reads a file one line at a time, useful for line-by-line processing.Step 2: Compare with other functions
fread()reads bytes,file()reads whole file into array,fopen()opens file but does not read.Final Answer:
fgets() -> Option DQuick Check:
Line-by-line reading = fgets() [OK]
- Confusing fread() with fgets()
- Thinking file() reads line by line
- Using fopen() to read data directly
$fp using fread()?Solution
Step 1: Recall fread() parameter order
fread()takes the file pointer first, then the number of bytes to read.Step 2: Validate syntax correctness
Onlyfread($fp, 100);matches the correct order and required parameters.Final Answer:
fread($fp, 100); -> Option CQuick Check:
fread(fp, bytes) syntax =fread($fp, 100);[OK]
- Swapping parameters order
- Omitting byte count
- Passing only one argument
data.txt contains:Line1 Line2 Line3
Code:
$lines = file('data.txt');
echo trim($lines[1]);Solution
Step 1: Understand file() output
file()reads the whole file into an array, each element is a line including newline.Step 2: Access second line and trim
$lines[1]is the second line "Line2\n";trim()removes newline, so output is "Line2".Final Answer:
Line2 -> Option AQuick Check:
file()[1] trimmed = Line2 [OK]
- Confusing array index starting at 0
- Not trimming newline characters
- Expecting fread() output instead
$fp = fopen('file.txt', 'r');
$content = fread(10, $fp);
echo $content;Solution
Step 1: Check fread() parameter order
fread()expects file pointer first, then number of bytes. Here they are reversed.Step 2: Confirm other parts
fopen('file.txt', 'r')is valid for reading text files; no third parameter needed; no info about fopen failure.Final Answer:
Parameters of fread() are reversed -> Option BQuick Check:
fread(fp, bytes) order matters [OK]
- Swapping fread() arguments
- Assuming fopen() mode error without evidence
- Expecting extra fread() parameters
Solution
Step 1: Consider memory and line processing
fgets()reads line by line, efficient for large files and allows checking each line.Step 2: Use stripos() for case-insensitive search
Inside the loop,stripos()finds "error" ignoring case, so only matching lines are printed.Final Answer:
Use fgets() in a loop, check each line with stripos() -> Option AQuick Check:
Line-by-line read + case-insensitive check = fgets() + stripos() [OK]
- Using fread() for line filtering
- Reading whole file into memory unnecessarily
- Printing file pointer instead of content
Ready to prove it?
Pass the test and this pattern moves unknown
