Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to extract the first 5 characters from the string.
PHP
<?php $text = "Hello, world!"; $part = substr($text, 0, [1]); echo $part; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong length value.
Confusing start position with length.
✗ Incorrect
The substr function extracts a part of the string starting at position 0 for 5 characters.
2fill in blank
mediumComplete the code to extract the substring starting from the 8th character to the end.
PHP
<?php $text = "Hello, world!"; $part = substr($text, [1]); echo $part; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 6 instead of 7 as start position.
Forgetting that positions start at 0.
✗ Incorrect
Starting at position 7 extracts the substring from the 8th character to the end.
3fill in blank
hardFix the error in the code to correctly extract 5 characters starting from the 3rd character.
PHP
<?php $text = "Programming"; $part = substr($text, [1], 5); echo $part; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 as start position which is actually the 4th character.
Confusing character count with position index.
✗ Incorrect
Position 2 is the 3rd character because counting starts at 0.
4fill in blank
hardFill both blanks to extract the last 4 characters of the string.
PHP
<?php $text = "Substring"; $part = substr($text, [1], [2]); echo $part; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive start position which extracts from the beginning.
Using wrong length value.
✗ Incorrect
Using a negative start position counts from the end. Length 4 extracts the last 4 characters.
5fill in blank
hardFill both blanks to filter for word lengths greater than 3.
PHP
<?php $words = ["apple", "cat", "banana", "dog"]; $lengths = array_filter(array_map(function($word) { return strlen($word); }, $words), function($len) { return $len [1] [2]; }); print_r($lengths); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or equal operators.
Confusing the comparison values.
✗ Incorrect
The filter keeps lengths greater than 3.