How to use Conditional Statements In PHP

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.



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

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.

Similar Reads

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....

Using an Array

...

Using Regular Expressions

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)....

Using a Switch Statement

...