0
0
PhpProgramBeginner · 2 min read

PHP Program to Check Vowel or Consonant

Use PHP to check if a character is a vowel by testing if it is in ['a','e','i','o','u'] (case-insensitive), else it is a consonant; for example, if (in_array(strtolower($char), ['a','e','i','o','u'])) { echo 'Vowel'; } else { echo 'Consonant'; }.
📋

Examples

Inputa
OutputVowel
InputB
OutputConsonant
Inputz
OutputConsonant
🧠

How to Think About It

To check if a character is a vowel or consonant, first convert it to lowercase to handle uppercase letters. Then compare it against the list of vowels a, e, i, o, u. If it matches any vowel, print 'Vowel'; otherwise, print 'Consonant'.
📐

Algorithm

1
Get the input character from the user.
2
Convert the character to lowercase.
3
Check if the character is in the list of vowels (a, e, i, o, u).
4
If yes, print 'Vowel'.
5
Otherwise, print 'Consonant'.
💻

Code

php
<?php
$char = 'A';
$charLower = strtolower($char);
$vowels = ['a', 'e', 'i', 'o', 'u'];
if (in_array($charLower, $vowels)) {
    echo "Vowel";
} else {
    echo "Consonant";
}
?>
Output
Vowel
🔍

Dry Run

Let's trace the input 'A' through the code

1

Input character

$char = 'A'

2

Convert to lowercase

$charLower = 'a'

3

Check if vowel

'a' is in ['a','e','i','o','u'] → true

4

Print result

Output: Vowel

StepVariableValue
1$char'A'
2$charLower'a'
3in_array checktrue
4OutputVowel
💡

Why This Works

Step 1: Convert to lowercase

Using strtolower() ensures the check works for both uppercase and lowercase letters.

Step 2: Check vowel membership

The in_array() function checks if the character is one of the vowels in the array.

Step 3: Output result

If the character is a vowel, print 'Vowel'; otherwise, print 'Consonant'.

🔄

Alternative Approaches

Using switch statement
php
<?php
$char = 'e';
switch (strtolower($char)) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        echo "Vowel";
        break;
    default:
        echo "Consonant";
}
?>
Switch is clear and readable but less flexible if vowels list changes.
Using regular expression
php
<?php
$char = 'I';
if (preg_match('/[aeiou]/i', $char)) {
    echo "Vowel";
} else {
    echo "Consonant";
}
?>
Regex is concise and handles case-insensitivity but may be less intuitive for beginners.

Complexity: O(1) time, O(1) space

Time Complexity

Checking membership in a small fixed array of vowels is constant time, O(1), since the array size does not grow.

Space Complexity

Only a small fixed array of vowels is stored, so space complexity is O(1).

Which Approach is Fastest?

Using in_array() or a switch statement both run in constant time and are efficient; regex is slightly slower but more flexible.

ApproachTimeSpaceBest For
in_array with arrayO(1)O(1)Simple and clear checks
switch statementO(1)O(1)Readable fixed cases
regular expressionO(1)O(1)Flexible pattern matching
💡
Always convert the input character to lowercase before checking to handle uppercase letters easily.
⚠️
Forgetting to convert the character to lowercase causes uppercase vowels to be misclassified as consonants.