We use string length and counting to find out how many characters are in a word or sentence. This helps us check or control text size.
String length and counting in PHP
Introduction
Syntax
PHP
$length = strlen($string);strlen() is a built-in PHP function that returns the number of characters in a string.
The count includes letters, numbers, spaces, and special characters.
Examples
PHP
$text = "Hello"; $length = strlen($text); echo $length;
PHP
$sentence = "Hi there!"; echo strlen($sentence);
PHP
$empty = ""; echo strlen($empty);
Sample Program
This program counts how many characters are in the string stored in $userInput and prints a friendly message.
PHP
<?php // Example: Count characters in a user input string $userInput = "Learn PHP!"; $length = strlen($userInput); echo "The string '" . $userInput . "' has " . $length . " characters."; ?>
Important Notes
Remember, strlen() counts all characters including spaces and punctuation.
For multibyte characters (like emojis or some foreign letters), strlen() may not count as expected. Use mb_strlen() for those cases.
Summary
strlen() tells you how many characters are in a string.
It counts everything: letters, spaces, and symbols.
Use it to check or control text size in your programs.
Practice
1. What does the PHP function
strlen() do?easy
Solution
Step 1: Understand the purpose of
Thestrlen()strlen()function counts all characters in a string, including letters, spaces, and symbols.Step 2: Compare with other options
Other options describe different string functions, notstrlen().Final Answer:
It returns the number of characters in a string. -> Option DQuick 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
Solution
Step 1: Identify the correct function for string length
In PHP,strlen()is the built-in function to get string length.Step 2: Check syntax correctness
strlen($text);correctly calls the function with the variable.Final Answer:
strlen($text); -> Option AQuick 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
Solution
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.Step 2: Confirm strlen() counts all characters
strlen($str)returns 12 because it counts every character.Final Answer:
12 -> Option AQuick 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
Solution
Step 1: Check function call syntax
In PHP, functions require parentheses around their arguments. Here,strlenis called without parentheses.Step 2: Verify other parts of the code
The variable name and string are correct, and semicolon is present after echo statement.Final Answer:
strlen() function call missing parentheses -> Option BQuick 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
Solution
Step 1: Use strlen() to get string length
The functionstrlen($input)returns the number of characters in the input string.Step 2: Compare length to 8 exactly
The conditionstrlen($input) == 8checks if the length is exactly 8 characters.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.Final Answer:
if (strlen($input) == 8) { echo "Valid length"; } else { echo "Invalid length"; } -> Option CQuick 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
10 questions ~5 mineasy hard
