Bird
0
0

Given the file data.txt contains "1234567890", what will this code output?

medium📝 Predict Output Q5 of 15
PHP - File Handling
Given the file data.txt contains "1234567890", what will this code output?
$fp = fopen('data.txt', 'r');
echo fread($fp, 4);
A123456
B1234
C123
D12345
Step-by-Step Solution
Solution:
  1. Step 1: Understand fread() parameters

    fread() reads the specified number of bytes from the file pointer.
  2. Step 2: Apply to given code

    It reads 4 bytes, so output is first 4 characters: "1234".
  3. Final Answer:

    1234 -> Option B
  4. Quick Check:

    fread() reads exact bytes requested = 1234 [OK]
Quick Trick: fread() reads exact byte count specified [OK]
Common Mistakes:
  • Reading one extra byte
  • Confusing fread() with fgets()
  • Assuming fread() reads whole file by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes