0
0
PHPprogramming~10 mins

String length and counting in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the length of the string.

PHP
<?php
$string = "hello";
$length = [1]($string);
echo $length;
?>
Drag options to blanks, or click blank then click option'
Astrlen
Bcount
Csize
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which works for arrays, not strings.
Trying to use length or size which are not PHP functions.
2fill in blank
medium

Complete the code to count how many times 'a' appears in the string.

PHP
<?php
$string = "banana";
$count = substr_count($string, [1]);
echo $count;
?>
Drag options to blanks, or click blank then click option'
A"b"
B"an"
C"n"
D"a"
Attempts:
3 left
💡 Hint
Common Mistakes
Counting the wrong character.
Not using quotes around the character.
3fill in blank
hard

Fix the error in the code to correctly get the string length.

PHP
<?php
$text = "apple";
$len = [1]($text);
echo $len;
?>
Drag options to blanks, or click blank then click option'
Acount
Bstrlen
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which causes wrong output.
Using non-existent functions like length or size.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

PHP
<?php
$words = ["cat", "house", "dog", "elephant"];
$lengths = [];
foreach ($words as $word) {
    if ([1]($word) [2] 3) {
        $lengths[$word] = strlen($word);
    }
}
print_r($lengths);
?>
Drag options to blanks, or click blank then click option'
Astrlen
Bcount
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() instead of strlen() for strings.
Using wrong comparison operators.
5fill in blank
hard

Fill all three blanks to create an associative array with uppercase keys and their original values for words longer than 4 characters.

PHP
<?php
$words = ["apple", "bat", "carrot", "dog"];
$result = [];
foreach ($words as $word) {
    if (strlen($word) [1] 4) {
        $result[[2]] = [3];
    }
}
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A>
B$word
Cstrtoupper($word)
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Mixing up keys and values in the array.