I would like to make a code that checks if the last character of a string ends with the letter "a" or "o" to identify whether the word is masculine or feminine.
I would like to make a code that checks if the last character of a string ends with the letter "a" or "o" to identify whether the word is masculine or feminine.
To get the last letter of a word use substr()
:
substr("testers", -1); // retorna "s"
If the 2nd parameter (known as start ) is negative, the returned string will start at the start character from the end of the string .
Then to check the condition assign to a variable and check whether the value is "a" or "o":
$ultima = substr("testers", -1);
if ($ultima == "a") {
echo "Feminina";
}
elseif($ultima == "o") {
echo "Masculina";
}else{
echo "A palavra não termina com 'a' ou 'o'";
}