0
0
PhpProgramBeginner · 2 min read

PHP Program to Count Vowels and Consonants

Use a PHP program that loops through each character of a string, checks if it is a vowel with in_array(), and counts vowels and consonants separately.
📋

Examples

Inputhello
OutputVowels: 2, Consonants: 3
InputPHP Programming
OutputVowels: 4, Consonants: 11
Input12345!@
OutputVowels: 0, Consonants: 0
🧠

How to Think About It

To count vowels and consonants, first convert the string to lowercase to treat letters uniformly. Then, check each character: if it is a letter, decide if it is a vowel by comparing it to a list of vowels. Increase vowel or consonant count accordingly. Ignore non-letter characters.
📐

Algorithm

1
Get the input string.
2
Convert the string to lowercase.
3
Initialize vowel and consonant counters to zero.
4
For each character in the string:
5
Check if it is a letter.
6
If it is a vowel, increase vowel count; else increase consonant count.
7
Print the counts of vowels and consonants.
💻

Code

php
<?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";
Output
Vowels: 4, Consonants: 11
🔍

Dry Run

Let's trace the string 'PHP Programming' through the code.

1

Convert to lowercase

Input 'PHP Programming' becomes 'php programming'

2

Initialize counters

vowelCount = 0, consonantCount = 0

3

Loop through each character

Check each character if letter and vowel or consonant

4

Count vowels and consonants

For example, 'p' consonant, 'h' consonant, 'p' consonant, ' ' ignored, 'r' consonant, 'o' vowel, etc.

CharacterIs Letter?Is Vowel?Vowel CountConsonant Count
pYesNo01
hYesNo02
pYesNo03
NoNo03
pYesNo04
rYesNo05
oYesYes15
gYesNo16
rYesNo17
aYesYes27
mYesNo28
mYesNo29
iYesYes39
nYesNo310
gYesNo311
💡

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

Using regular expressions
php
<?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";
This method uses regex to count vowels and consonants directly, which is concise but may be less clear for beginners.
Using array filter and str_split
php
<?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";
This approach uses functional programming style with array_filter, which is elegant but may be harder for absolute beginners.

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.

ApproachTimeSpaceBest For
Loop with in_arrayO(n)O(1)Clarity and beginner-friendly
Regular expressionsO(n)O(1)Concise code, regex familiarity
Array filter with callbacksO(n)O(n)Functional style, readability for advanced users
💡
Always convert the string to lowercase before checking vowels to simplify comparisons.
⚠️
Counting non-letter characters like spaces or punctuation as consonants or vowels.