PHP program to check whether input character is vowel or consonant using if else
ADVERTISEMENTS
In this article, you will learn how to make a PHP program to check whether the input character is vowel or consonant using if else in the PHP programming language.
Example
'O' is a vowel character.
You should have knowledge of the following topics in PHP programming to understand this program:
- PHP Ctype functions
- PHP
if-else
condition statement
Vowel Characters
A, E, I, O, U, and a, e, i, o, u are the vowel characters.
Consonant Characters
B, C, ...., Y, Z, and b, c, .., y, z are the consonant characters.
Source Code
<?php
// PHP program to check whether input character is vowel or consonant using if-else
$x = "O";
$l = 0;
$u = 0;
// $l denotes for lowercase vowels
// $u denotes for uppercase vowels
// ctype_alpha() function checks
// whether character is alphabetic or non-alphabetic
if ($x && ctype_alpha($x)) {
// this will return 1 if input character matched with these small characters
$l = $x == 'a' || $x == 'e' || $x == 'i' || $x == 'o' || $x == 'u';
// this will return 1 if input character matched with these capital characters
$u = $x == 'A' || $x == 'E' || $x == 'I' || $x == 'O' || $x == 'U';
if ($l || $u)
echo "'" . $x . "' is a vowel character.\n";
else
echo "'" . $x . "' is a consonant character.\n";
} else {
echo "Sorry, You entered Non-Alphabetic character!\n";
}
?>
Output
'O' is a vowel character.
Explanation
In this program, we have stored input K
in the $x
variable and checked for its alphabetic or non-alphabetic character.
Then we matched this character with a series of vowel characters, If matched with the vowel characters series then it's vowel character.
If didn't match with the vowel characters series then It's a consonant character.