PHP Program to Count Vowels and Consonants
in_array(), and counts vowels and consonants separately.Examples
How to Think About It
Algorithm
Code
<?php $input = "PHP Programming"; $vowels = ['a', 'e', 'i', 'o', 'u']; $vowelCount = 0; $consonantCount = 0; $inputLower = strtolower($input); for ($i = 0; $i < strlen($inputLower); $i++) { $char = $inputLower[$i]; if (ctype_alpha($char)) { if (in_array($char, $vowels)) { $vowelCount++; } else { $consonantCount++; } } } echo "Vowels: $vowelCount, Consonants: $consonantCount";
Dry Run
Let's trace the string 'PHP Programming' through the code.
Convert to lowercase
Input 'PHP Programming' becomes 'php programming'
Initialize counters
vowelCount = 0, consonantCount = 0
Loop through each character
Check each character if letter and vowel or consonant
Count vowels and consonants
For example, 'p' consonant, 'h' consonant, 'p' consonant, ' ' ignored, 'r' consonant, 'o' vowel, etc.
| Character | Is Letter? | Is Vowel? | Vowel Count | Consonant Count |
|---|---|---|---|---|
| p | Yes | No | 0 | 1 |
| h | Yes | No | 0 | 2 |
| p | Yes | No | 0 | 3 |
| No | No | 0 | 3 | |
| p | Yes | No | 0 | 4 |
| r | Yes | No | 0 | 5 |
| o | Yes | Yes | 1 | 5 |
| g | Yes | No | 1 | 6 |
| r | Yes | No | 1 | 7 |
| a | Yes | Yes | 2 | 7 |
| m | Yes | No | 2 | 8 |
| m | Yes | No | 2 | 9 |
| i | Yes | Yes | 3 | 9 |
| n | Yes | No | 3 | 10 |
| g | Yes | No | 3 | 11 |
Why This Works
Step 1: Lowercase conversion
Converting the string to lowercase with strtolower() makes it easier to compare characters without case mismatch.
Step 2: Check letters only
Using ctype_alpha() ensures only alphabetic characters are counted, ignoring spaces or symbols.
Step 3: Count vowels and consonants
Using in_array() to check vowels lets us increment vowel or consonant counters correctly.
Alternative Approaches
<?php $input = "PHP Programming"; $vowelCount = preg_match_all('/[aeiou]/i', $input, $matches); $consonantCount = preg_match_all('/[bcdfghjklmnpqrstvwxyz]/i', $input, $matches); echo "Vowels: $vowelCount, Consonants: $consonantCount";
<?php $input = "PHP Programming"; $chars = str_split(strtolower($input)); $vowels = ['a','e','i','o','u']; $vowelCount = count(array_filter($chars, fn($c) => in_array($c, $vowels))); $consonantCount = count(array_filter($chars, fn($c) => ctype_alpha($c) && !in_array($c, $vowels))); echo "Vowels: $vowelCount, Consonants: $consonantCount";
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through each character once, so time grows linearly with string length.
Space Complexity
Only a few counters and a small vowels array are used, so space is constant.
Which Approach is Fastest?
The loop with in_array() is efficient and clear; regex may be slightly slower but more concise.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Loop with in_array | O(n) | O(1) | Clarity and beginner-friendly |
| Regular expressions | O(n) | O(1) | Concise code, regex familiarity |
| Array filter with callbacks | O(n) | O(n) | Functional style, readability for advanced users |