0
0
PHPprogramming~20 mins

Why file operations matter in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP file write and read code?
Consider the following PHP code that writes to a file and then reads from it. What will be printed?
PHP
<?php
$file = 'test.txt';
file_put_contents($file, "Hello World!");
$content = file_get_contents($file);
echo $content;
?>
ANULL
Btest.txt
CHello World!
DError: file not found
Attempts:
2 left
💡 Hint
file_put_contents writes text to a file, file_get_contents reads it back.
🧠 Conceptual
intermediate
1:30remaining
Why is it important to close a file after opening it in PHP?
Which of the following best explains why closing a file after opening it is important?
AIt deletes the file from the disk.
BIt frees system resources and ensures data is properly saved.
CIt encrypts the file contents automatically.
DIt makes the file read-only.
Attempts:
2 left
💡 Hint
Think about what happens if too many files stay open.
🔧 Debug
advanced
2:00remaining
What error does this PHP file open code produce?
Look at this PHP code snippet. What error will it cause when run if the file does not exist?
PHP
<?php
$handle = fopen('nofile.txt', 'r');
?>
AWarning: fopen(): failed to open stream: No such file or directory
BFatal error: Cannot redeclare fopen()
CParse error: syntax error, unexpected 'fopen'
DNo error, file is created automatically
Attempts:
2 left
💡 Hint
Opening a file for reading requires the file to exist.
📝 Syntax
advanced
2:00remaining
Which option correctly appends text to a file in PHP?
You want to add text to the end of a file without deleting existing content. Which code snippet does this correctly?
A<?php fopen('log.txt', 'w'); fwrite("New entry\n"); fclose(); ?>
B<?php file_put_contents('log.txt', "New entry\n"); ?>
C<?php fopen('log.txt', 'r+'); fwrite("New entry\n"); fclose(); ?>
D<?php file_put_contents('log.txt', "New entry\n", FILE_APPEND); ?>
Attempts:
2 left
💡 Hint
Appending means adding without removing old content.
🚀 Application
expert
2:30remaining
How many lines will this PHP script write to the file?
This PHP script writes lines to a file inside a loop. How many lines will the file contain after running?
PHP
<?php
$file = 'output.txt';
$handle = fopen($file, 'w');
for ($i = 1; $i <= 5; $i++) {
    fwrite($handle, "Line $i\n");
}
fclose($handle);
?>
A5
B1
C0
D6
Attempts:
2 left
💡 Hint
Count how many times the loop runs and writes a line.