File open modes in PHP - Time & Space Complexity
When working with files in PHP, the way you open a file affects how long your program takes to run.
We want to understand how the time to open and read a file changes as the file size grows.
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 opens a file for reading, reads it line by line, and prints each line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each line from the file inside the while loop.
- How many times: Once for every line in the file, depending on file size.
As the file gets bigger, the program reads more lines, so it takes longer.
| Input Size (lines) | Approx. Operations (reads) |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: The time 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.
[X] Wrong: "Opening a file is always a constant time operation regardless of file size."
[OK] Correct: Opening a file is usually quick, but reading depends on file size, so total time grows with how much you read.
Understanding how file reading time grows helps you write programs that handle big files without slowing down too much.
"What if we changed reading line by line to reading the whole file at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of each mode
rmode opens a file for reading only, without modifying it. Other modes allow writing or creating files.Step 2: Match the mode to the requirement
Since the question asks for reading without changing,ris the correct mode.Final Answer:
r - Open for reading only -> Option AQuick Check:
Read only = r [OK]
- Choosing 'w' which overwrites the file
- Confusing 'a' (append) with read mode
- Using 'x' which creates a new file
Solution
Step 1: Recall the mode for writing
The mode'w'opens a file for writing and truncates it if it exists.Step 2: Check syntax correctness
fopen('file.txt', 'w')is the correct syntax to open for writing.Final Answer:
fopen('file.txt', 'w') -> Option DQuick Check:
Write mode = 'w' [OK]
- Using 'r' which is read-only
- Using 'rw' which is invalid in PHP
- Confusing 'a' (append) with write
$file = fopen('test.txt', 'a');
fwrite($file, "Hello\n");
fclose($file);
echo file_get_contents('test.txt');Solution
Step 1: Understand the 'a' mode effect
Mode 'a' opens the file for appending, so "Hello\n" (string with actual newline) is added at the end.Step 2: Check output of file_get_contents
It reads the whole file content including the newline, and echo prints it with the newline interpreted.Final Answer:
Hello -> Option AQuick Check:
"Hello\n" adds a newline, so output is Hello followed by newline [OK]
- Thinking '\n' becomes literal backslash-n
- Expecting 'Hello' without newline
- Assuming error without write permission
$file = fopen('data.txt', 'r');
fwrite($file, 'Test');
fclose($file);Solution
Step 1: Check file open mode
The file is opened in 'r' mode which is read-only and does not allow writing.Step 2: Understand fwrite limitation
Calling fwrite on a read-only file causes an error because writing is not permitted.Final Answer:
Cannot write to a file opened in 'r' mode -> Option CQuick Check:
Write needs 'w' or 'a' mode [OK]
- Ignoring mode restrictions
- Assuming fclose fixes write errors
- Confusing syntax errors with mode errors
Solution
Step 1: Understand each mode's behavior
r+reads/writes but fails if file missing;w+truncates file;a+reads/writes, creates if missing, appends writes;x+creates new file only.Step 2: Match requirements
We want read/write, no truncation, and create if missing.a+fits all: it opens for reading and appending, creates file if missing, and does not truncate.Final Answer:
a+ -> Option BQuick Check:
Read/write + create + no truncate = a+ [OK]
- Choosing 'r+' which fails if file missing
- Choosing 'w+' which truncates file
- Confusing 'x+' which fails if file exists
Ready to prove it?
Pass the test and this pattern moves unknown
