Consider this PHP code that reads from a file named example.txt containing the text Hello World!:
$file = fopen('example.txt', 'r');
$content = fread($file, 5);
fclose($file);
echo $content;What will be printed?
$file = fopen('example.txt', 'r'); $content = fread($file, 5); fclose($file); echo $content;
Remember, fread reads a specific number of bytes from the file.
The fread function reads exactly 5 bytes from the file. Since the file contains "Hello World!", reading 5 bytes returns "Hello".
Given a file data.txt with the content:
Line one Line two Line three
What will this PHP code output?
$file = fopen('data.txt', 'r');
$line = fgets($file);
fclose($file);
echo $line;$file = fopen('data.txt', 'r'); $line = fgets($file); fclose($file); echo $line;
Check if fgets includes the newline character at the end of the line.
The fgets function reads a line including the newline character at the end. So it returns "Line one\n" (newline character included).
Assume notes.txt contains:
First line Second line Third line
What will this PHP code output?
$lines = file('notes.txt');
echo count($lines);$lines = file('notes.txt'); echo count($lines);
The file() function reads the file into an array, one element per line.
The file() function returns an array where each element is a line from the file. Since there are 3 lines, count($lines) returns 3.
What happens when running this PHP code?
$file = fopen('missing.txt', 'r');
fgets($file);$file = fopen('missing.txt', 'r'); fgets($file);
Check what happens if you try to open a file that does not exist in read mode.
Trying to open a non-existent file in read mode causes a warning from fopen indicating the file cannot be found.
You want to read the entire content of story.txt into a single string variable in PHP. Which code snippet does this correctly?
Look for the function that reads the whole file as a string in one call.
file_get_contents() reads the entire file content into a string. Option D reads line by line and concatenates, which works but is more complex. Option D returns an array, not a string. Option D is invalid because fread requires a file handle and length.