PHP program to Check Vowel or Consonant

In this example, we are going to see how to write a simple PHP program to check if the entered alphabet is a Vowel or a Consonant.

I hope you know the Vowels in English, if you don’t, these are the Vowels in English (A, E, I, O, U). Other letters are Consonants.

Here is the complete PHP program with inline comments,

<!DOCTYPE html>
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>PHP program to Check Vowel or Consonant</title>
   </head>
   <body>
       <h3>PHP program to Check Vowel or Consonant</h3>
       <form action="" method="post">
        Enter the alphabet: <input type="text" size="1" name="alphabet" maxlength="1" onkeypress="return (event.charCode > 64 && event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || (event.charCode==32)" /><br /><br />
       <input type="submit" name="submit" /><br /><br />
      </form>
   </body>
</html>
<?php 

 if (isset($_POST['submit'])) {
    
    //set the vowels in an array
    $vowels = ['a','e','i','o','u'];
    $alphabet = $_POST['alphabet'];

    //evaluate if the user entered alphabet is in the array
    //if yes, then it is a Vowel or it is a Constant
    if (in_array(strtolower($alphabet), $vowels)) {
        echo $alphabet." is a Vowel";
    } else {
        echo $alphabet." is a Consonant";
    }
 }

?>

It’s a very simple program, so please go through the above lines of code to understand it.

You can also see the demo of this program by clicking the below link:

demo

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top