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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!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: