0
0
PHPprogramming~20 mins

String search functions (strpos, strstr) in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Search 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 strpos with needle at start?
Consider the following PHP code snippet. What will be the output when searching for the substring 'Hello' in the string 'Hello World' using strpos?
PHP
<?php
$pos = strpos('Hello World', 'Hello');
echo $pos;
?>
Afalse
B1
Cnull
D0
Attempts:
2 left
💡 Hint
Remember that strpos returns the position index starting at 0.
Predict Output
intermediate
2:00remaining
What does strstr return when substring is found?
What will be the output of this PHP code using strstr to find 'World' in 'Hello World'?
PHP
<?php
$result = strstr('Hello World', 'World');
echo $result;
?>
AWorld
BHello World
Corld
Dfalse
Attempts:
2 left
💡 Hint
strstr returns the part of the string starting from the first occurrence of the needle.
Predict Output
advanced
2:00remaining
What is the output when strpos needle is not found?
What will this PHP code output when searching for 'abc' in 'defgh' using strpos?
PHP
<?php
$pos = strpos('defgh', 'abc');
var_dump($pos);
?>
A0
Bbool(false)
Cint(0)
Dnull
Attempts:
2 left
💡 Hint
If the substring is not found, strpos returns false.
Predict Output
advanced
2:00remaining
What does strstr return with before_needle=true?
What will this PHP code output when using strstr with the third parameter true to find 'World' in 'Hello World'?
PHP
<?php
$result = strstr('Hello World', 'World', true);
echo $result;
?>
AHello
BHello World
CWorld
Dfalse
Attempts:
2 left
💡 Hint
When before_needle is true, strstr returns the part before the needle.
🧠 Conceptual
expert
2:00remaining
Why strpos can return 0 but still be true in condition?
In PHP, why does this code print 'Not Found' even though strpos returns 0?
ABecause 0 is treated as false, so it prints 'Found' anyway
BBecause the condition treats 0 as false, so it prints 'Not Found', but the code prints 'Found' due to a bug
CBecause strpos returns false on failure, and 0 is treated as false, so it prints 'Not Found'
DBecause 0 is treated as false, so it prints 'Not Found'
Attempts:
2 left
💡 Hint
Check how PHP treats 0 and false in if conditions.