Challenge - 5 Problems
String Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; ?>
Attempts:
2 left
💡 Hint
Remember that strpos returns the position index starting at 0.
✗ Incorrect
The substring 'Hello' starts at the very beginning of the string, so strpos returns 0, which is a valid position.
❓ Predict Output
intermediate2: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; ?>
Attempts:
2 left
💡 Hint
strstr returns the part of the string starting from the first occurrence of the needle.
✗ Incorrect
strstr returns the substring starting from 'World' to the end, which is 'World'.
❓ Predict Output
advanced2: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); ?>
Attempts:
2 left
💡 Hint
If the substring is not found, strpos returns false.
✗ Incorrect
Since 'abc' is not in 'defgh', strpos returns boolean false, and var_dump shows bool(false).
❓ Predict Output
advanced2: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; ?>
Attempts:
2 left
💡 Hint
When before_needle is true, strstr returns the part before the needle.
✗ Incorrect
With before_needle=true, strstr returns the substring before 'World', which is 'Hello '.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Check how PHP treats 0 and false in if conditions.
✗ Incorrect
strpos returns 0 when the substring is at the start, but 0 is false in PHP conditions, so the if condition fails and prints 'Not Found'. The code prints 'Found' only if strpos returns a non-false, non-zero value.