0
0
PHPprogramming~10 mins

Why file operations matter in PHP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file for reading.

PHP
<?php
$file = fopen('example.txt', '[1]');
?>
Drag options to blanks, or click blank then click option'
Aw
Br
Ca
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' which opens the file for writing and erases content.
2fill in blank
medium

Complete the code to write 'Hello' to a file.

PHP
<?php
$file = fopen('greeting.txt', 'w');
fwrite([1], 'Hello');
fclose($file);
?>
Drag options to blanks, or click blank then click option'
A$file
B$handle
C$open
D$f
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not defined or different from the fopen result.
3fill in blank
hard

Fix the error in the code to read a file line by line.

PHP
<?php
$file = fopen('data.txt', 'r');
while (($line = fgets([1])) !== false) {
    echo $line;
}
fclose($file);
?>
Drag options to blanks, or click blank then click option'
A$f
B$handle
C$file
D$stream
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not assigned or undefined in the code.
4fill in blank
hard

Fill both blanks to check if a file exists and then delete it.

PHP
<?php
if ([1]('temp.txt')) {
    [2]('temp.txt');
}
?>
Drag options to blanks, or click blank then click option'
Afile_exists
Bunlink
Cfopen
Dreadfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using fopen instead of file_exists for checking file presence.
5fill in blank
hard

Fill all three blanks to open a file, write text, and close it properly.

PHP
<?php
[1] = fopen('log.txt', '[2]');
fwrite([3], "Log entry\n");
fclose($file);
?>
Drag options to blanks, or click blank then click option'
A$file
Ba
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' mode erases file content instead of appending.