Bird
Raised Fist0
PHPprogramming~10 mins

String length and counting in PHP - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the PHP function strlen() do?
easy
A. It removes spaces from a string.
B. It converts a string to uppercase.
C. It splits a string into an array.
D. It returns the number of characters in a string.

Solution

  1. Step 1: Understand the purpose of strlen()

    The strlen() function counts all characters in a string, including letters, spaces, and symbols.
  2. Step 2: Compare with other options

    Other options describe different string functions, not strlen().
  3. Final Answer:

    It returns the number of characters in a string. -> Option D
  4. Quick Check:

    strlen() counts characters [OK]
Hint: Remember: strlen() counts all characters in a string [OK]
Common Mistakes:
  • Thinking strlen() changes the string
  • Confusing strlen() with string splitting
  • Assuming it removes spaces
2. Which of the following is the correct syntax to get the length of a string stored in variable $text?
easy
A. strlen($text);
B. size($text);
C. count($text);
D. length($text);

Solution

  1. Step 1: Identify the correct function for string length

    In PHP, strlen() is the built-in function to get string length.
  2. Step 2: Check syntax correctness

    strlen($text); correctly calls the function with the variable.
  3. Final Answer:

    strlen($text); -> Option A
  4. Quick Check:

    Use strlen() with parentheses and variable [OK]
Hint: Use strlen() with parentheses around the variable [OK]
Common Mistakes:
  • Using length() which is not a PHP function
  • Using count() which is for arrays
  • Forgetting parentheses
3. What will be the output of the following PHP code?
$str = "Hello World!";
echo strlen($str);
medium
A. 12
B. 10
C. 11
D. 13

Solution

  1. Step 1: Count characters in the string including spaces and punctuation

    The string "Hello World!" has 12 characters: 5 letters + 1 space + 5 letters + 1 exclamation mark.
  2. Step 2: Confirm strlen() counts all characters

    strlen($str) returns 12 because it counts every character.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    Count all characters including spaces and punctuation [OK]
Hint: Count every character including spaces and symbols [OK]
Common Mistakes:
  • Ignoring spaces in count
  • Forgetting punctuation counts
  • Assuming strlen() excludes special characters
4. Identify the error in the following PHP code that tries to print the length of a string:
$text = "PHP Rocks!";
echo strlen $text;
medium
A. Missing semicolon after echo
B. strlen() function call missing parentheses
C. Variable name is incorrect
D. String is not enclosed in quotes

Solution

  1. Step 1: Check function call syntax

    In PHP, functions require parentheses around their arguments. Here, strlen is called without parentheses.
  2. Step 2: Verify other parts of the code

    The variable name and string are correct, and semicolon is present after echo statement.
  3. Final Answer:

    strlen() function call missing parentheses -> Option B
  4. Quick Check:

    Always use parentheses when calling functions [OK]
Hint: Functions need parentheses even with one argument [OK]
Common Mistakes:
  • Omitting parentheses in function calls
  • Confusing variable names
  • Missing semicolon after statements
5. You want to check if a user's input stored in $input is exactly 8 characters long. Which PHP code correctly performs this check?
hard
A. if (strlen($input) > 8) { echo "Valid length"; } else { echo "Invalid length"; }
B. if (count($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; }
C. if (strlen($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; }
D. if (strlen($input) != 8) { echo "Valid length"; } else { echo "Invalid length"; }

Solution

  1. Step 1: Use strlen() to get string length

    The function strlen($input) returns the number of characters in the input string.
  2. Step 2: Compare length to 8 exactly

    The condition strlen($input) == 8 checks if the length is exactly 8 characters.
  3. Step 3: Understand other options

    if (count($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; } uses count() which is for arrays, not strings. if (strlen($input) > 8) { echo "Valid length"; } else { echo "Invalid length"; } checks if length is greater than 8, not equal. if (strlen($input) != 8) { echo "Valid length"; } else { echo "Invalid length"; } reverses logic.
  4. Final Answer:

    if (strlen($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; } -> Option C
  5. Quick Check:

    Use strlen() == 8 to check exact length [OK]
Hint: Use strlen() == 8 to check exact string length [OK]
Common Mistakes:
  • Using count() on strings
  • Using > or != instead of == for exact match
  • Confusing string length with array size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard