Open In App

PHP Program to Check if a Given Letter is Vowel or Consonant

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Determining whether a given letter is a vowel or consonant is a fundamental programming task that can be approached in various ways using PHP. This article explores multiple methods to achieve this, catering to beginners and experienced developers alike.

Understanding Vowels and Consonants

Before diving into the code, it’s crucial to understand what vowels and consonants are. In the English alphabet, the letters A, E, I, O, and U are considered vowels, and the rest are consonants. This distinction is essential for linguistic processing, text analysis, and even simple input validation in web forms or applications.

Using Conditional Statements

The simplest way to check if a given letter is a vowel or consonant is by using conditional statements. This approach is straightforward to understand for beginners.

Example:

PHP




<?php
 
function isVowel($letter)
{
    $letter = strtolower($letter);
    if (
        $letter == "a" ||
        $letter == "e" ||
        $letter == "i" ||
        $letter == "o" ||
        $letter == "u"
    ) {
        return true;
    } else {
        return false;
    }
}
 
// Driver code
$letter = "E";
if (isVowel($letter)) {
    echo "$letter is a vowel.";
} else {
    echo "$letter is a consonant.";
}
 
?>


Output

E is a vowel.



Using an Array

Another method to determine if a letter is a vowel or consonant is by storing the vowels in an array and checking if the given letter exists in that array. This approach makes the code cleaner and easier to maintain, especially if the criteria change (e.g., considering ‘y’ as a vowel in certain contexts).

Example:

PHP




<?php
 
function isVowelArray($letter)
{
    $vowels = ["a", "e", "i", "o", "u"];
    return in_array(strtolower($letter), $vowels);
}
 
// Driver code
$letter = "A";
if (isVowelArray($letter)) {
    echo "$letter is a vowel.";
} else {
    echo "$letter is a consonant.";
}
 
?>


Output

A is a vowel.



Using Regular Expressions

For those familiar with regular expressions, this method offers a concise and powerful way to check if a letter is a vowel or consonant. Regular expressions are particularly useful when the criteria for classification are complex or when validating patterns in strings.

Example:

PHP




<?php
 
function isVowelRegex($letter)
{
    return preg_match("/[aeiou]/i", $letter) === 1;
}
 
// Driver code
$letter = "i";
if (isVowelRegex($letter)) {
    echo "$letter is a vowel.";
} else {
    echo "$letter is a consonant.";
}
 
?>


Output

i is a vowel.



Using a Switch Statement

A switch statement can also be used to check if a letter is a vowel or consonant. This approach is similar to using conditional statements but can be more readable when dealing with multiple discrete cases.

Example:

PHP




<?php
 
function isVowelSwitch($letter)
{
    $letter = strtolower($letter);
    switch ($letter) {
        case "a":
        case "e":
        case "i":
        case "o":
        case "u":
            return true;
        default:
            return false;
    }
}
 
// Driver code
$letter = "O";
if (isVowelSwitch($letter)) {
    echo "$letter is a vowel.";
} else {
    echo "$letter is a consonant.";
}
?>


Output

O is a vowel.





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads