0
0
PHPprogramming~20 mins

Substring extraction in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Substring Extraction 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 substring extraction?
Consider the following PHP code. What will it output?
PHP
<?php
$text = "Hello, world!";
echo substr($text, 7, 5);
?>
Aworld
Bworld!
CHello
Do, wo
Attempts:
2 left
💡 Hint
Remember that substr starts at the offset and extracts the given length.
Predict Output
intermediate
2:00remaining
What substring does this code extract?
What will be the output of this PHP code snippet?
PHP
<?php
$str = "Programming";
echo substr($str, -6, 3);
?>
Aram
Bamm
Cogr
Ding
Attempts:
2 left
💡 Hint
Negative start means counting from the end of the string.
Predict Output
advanced
2:00remaining
What is the output when length exceeds string end?
What will this PHP code output?
PHP
<?php
$s = "OpenAI";
echo substr($s, 3, 10);
?>
AnAI
BnAI"
CenAI
DSyntaxError
Attempts:
2 left
💡 Hint
If length goes beyond string end, substr returns until the end.
Predict Output
advanced
2:00remaining
What happens if start is beyond string length?
What will this PHP code output?
PHP
<?php
$str = "Data";
echo substr($str, 10, 2);
?>
Ata
BDa
CError: Out of bounds
D"" (empty string)
Attempts:
2 left
💡 Hint
If start is beyond string length, substr returns empty string.
Predict Output
expert
3:00remaining
What is the output of this nested substring extraction?
Analyze the code and find the output:
PHP
<?php
$text = "abcdefg";
$part = substr($text, 2, 4);
echo substr($part, 1, 2);
?>
Acd
Bbc
Cde
Def
Attempts:
2 left
💡 Hint
First extract 4 chars from index 2, then extract 2 chars from index 1 of that result.