Bird
0
0

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📝 Application Q15 of 15
PHP - String Functions
You want to check if a user's input stored in $input is exactly 8 characters long. Which PHP code correctly performs this check?
Aif (strlen($input) > 8) { echo "Valid length"; } else { echo "Invalid length"; }
Bif (count($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; }
Cif (strlen($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; }
Dif (strlen($input) != 8) { echo "Valid length"; } else { echo "Invalid length"; }
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes