0
0
PHPprogramming~20 mins

File pointer manipulation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Pointer Master
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 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);
?>
A6
B11
C0
D5
Attempts:
2 left
💡 Hint
ftell() returns the current position of the file pointer after fseek().
Predict Output
intermediate
2: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);
?>
Acde
Babc
Cbcd
Ddef
Attempts:
2 left
💡 Hint
fseek moves pointer to position 2, fread reads 3 bytes from there.
Predict Output
advanced
2: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);
?>
A5
B4
C0
D1
Attempts:
2 left
💡 Hint
rewind() moves the pointer back to the start of the file.
Predict Output
advanced
2: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);
?>
AWarning: fwrite() expects parameter 1 to be resource, bool given
BWarning: fwrite(): supplied resource is not writable
CNo output, file pointer moved and wrote successfully
DFatal error: fseek() expects parameter 1 to be resource
Attempts:
2 left
💡 Hint
The file is opened in read-only mode but fwrite is called.
🧠 Conceptual
expert
3: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;
?>
A7,11
B7,10
C10,14
D6,10
Attempts:
2 left
💡 Hint
fread reads up to the end of file or requested bytes, pointer moves accordingly.