Challenge - 5 Problems
File Pointer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code using file pointer?
Consider the following PHP code that reads a file pointer position after some operations. What will it output?
PHP
<?php $file = fopen('php://memory', 'r+'); fwrite($file, "Hello World"); fseek($file, 6); echo ftell($file); fclose($file); ?>
Attempts:
2 left
💡 Hint
ftell() returns the current position of the file pointer after fseek().
✗ Incorrect
The code writes 'Hello World' (11 characters) to the file, then moves the pointer to position 6 with fseek(). ftell() then returns 6, the current pointer position.
❓ Predict Output
intermediate2:00remaining
What does this PHP code output after reading with file pointer?
Given this PHP code, what will be printed?
PHP
<?php $file = fopen('php://memory', 'r+'); fwrite($file, "abcdef"); fseek($file, 2); echo fread($file, 3); fclose($file); ?>
Attempts:
2 left
💡 Hint
fseek moves pointer to position 2, fread reads 3 bytes from there.
✗ Incorrect
The string 'abcdef' is written. Pointer moves to position 2 (0-based), which is 'c'. Reading 3 bytes from there gives 'cde'.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code using rewind and ftell?
Analyze this PHP code snippet. What will it output?
PHP
<?php $file = fopen('php://memory', 'r+'); fwrite($file, "12345"); fseek($file, 4); rewind($file); echo ftell($file); fclose($file); ?>
Attempts:
2 left
💡 Hint
rewind() moves the pointer back to the start of the file.
✗ Incorrect
After writing '12345', pointer moves to position 4. rewind() resets pointer to 0. ftell() then returns 0.
❓ Predict Output
advanced2:00remaining
What error or output does this PHP code produce?
What happens when this PHP code runs?
PHP
<?php $file = fopen('php://memory', 'r'); fseek($file, 5); fwrite($file, "test"); fclose($file); ?>
Attempts:
2 left
💡 Hint
The file is opened in read-only mode but fwrite is called.
✗ Incorrect
Opening with 'r' mode means read-only. fwrite() on a read-only stream triggers a warning about the resource not being writable.
🧠 Conceptual
expert3:00remaining
How many bytes are read and what is the pointer position after this PHP code?
Consider this PHP code snippet. How many bytes does fread() read, and what is the file pointer position after reading?
PHP
<?php $file = fopen('php://memory', 'r+'); fwrite($file, "OpenAI GPT"); fseek($file, 4); $data = fread($file, 10); $pos = ftell($file); fclose($file); echo strlen($data) . ',' . $pos; ?>
Attempts:
2 left
💡 Hint
fread reads up to the end of file or requested bytes, pointer moves accordingly.
✗ Incorrect
The string 'OpenAI GPT' is 10 bytes. Pointer moves to 4 ('A'). Reading 10 bytes but only 6 bytes remain to EOF ('AI GPT'), so fread reads 6 bytes. Pointer moves to position 10.