Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a file for reading.
PHP
<?php $handle = fopen('example.txt', '[1]'); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which opens the file for writing and truncates it.
Using 'a' which opens the file for appending.
✗ Incorrect
The mode 'r' opens the file for reading only.
2fill in blank
mediumComplete the code to read 100 bytes from the file.
PHP
<?php $handle = fopen('example.txt', 'r'); $content = fread($handle, [1]); fclose($handle); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 which reads nothing.
Using a smaller number like 50 which reads less than needed.
✗ Incorrect
fread reads the specified number of bytes; here, 100 bytes.
3fill in blank
hardFix the error in the code to read one line from the file.
PHP
<?php $handle = fopen('example.txt', 'r'); $line = [1]($handle); fclose($handle); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fread which reads bytes, not lines.
Using file_get_contents which reads the whole file.
✗ Incorrect
fgets reads one line from the file handle.
4fill in blank
hardFill both blanks to read the entire file into an array of lines and print the first line.
PHP
<?php $lines = [1]('example.txt'); [2] $lines[0]; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using fgets instead of file to read all lines.
Using print_r which prints array structure, not just the line.
✗ Incorrect
The file function reads the file into an array of lines. echo prints the first line.
5fill in blank
hardFill all three blanks to open a file, read its entire content, and close it.
PHP
<?php $handle = [1]('example.txt', 'r'); $content = [2]($handle, filesize('example.txt')); fclose([3]); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using file_get_contents instead of fread when a handle is used.
Not closing the file handle.
✗ Incorrect
Use fopen to open the file, fread to read the content, and fclose to close the file handle.