How do you know if the last character of a string ends with the letter "a" or "o"?

1

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.

    
asked by anonymous 09.11.2017 / 22:16

1 answer

3

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'";
}
    
09.11.2017 / 22:22