Bird
0
0

Identify the error in this PHP code snippet that tries to read the first 10 bytes of a file:

medium📝 Debug Q14 of 15
PHP - File Handling
Identify the error in this PHP code snippet that tries to read the first 10 bytes of a file:
$fp = fopen('file.txt', 'r');
$content = fread(10, $fp);
echo $content;
Afopen() mode should be 'rb' instead of 'r'
BParameters of fread() are reversed
Cfread() requires a third parameter
Dfopen() returns false, file not opened
Step-by-Step Solution
Solution:
  1. Step 1: Check fread() parameter order

    fread() expects file pointer first, then number of bytes. Here they are reversed.
  2. Step 2: Confirm other parts

    fopen('file.txt', 'r') is valid for reading text files; no third parameter needed; no info about fopen failure.
  3. Final Answer:

    Parameters of fread() are reversed -> Option B
  4. Quick Check:

    fread(fp, bytes) order matters [OK]
Quick Trick: fread() needs file pointer first, then bytes [OK]
Common Mistakes:
  • Swapping fread() arguments
  • Assuming fopen() mode error without evidence
  • Expecting extra fread() parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes