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

Asked

Viewed 1,130 times

1

I would like to make a code that sees if the last character of a string ends with the letter "a" or "o" to identify whether the word is male or female.

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) for 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 "the":

$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'";
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.