What is the simplest way of checking whether the first letter of a string $str
is 'a'
and the last letter is 'a'
too?
Using substr() Method: The substr() is a built-in function in PHP that is used to extract a part of string. Example: For example, if the string is “Akshit loves GeeksForGeeks”. The last character of the string is “s”.
We can test if a certain string is the exact start of another string: <? php function startsWith($string, $startString) { $len = strlen($startString); return (substr($string, 0, $len) === $startString); } // usage echo startsWith("cat", "c"); // true echo startsWith("dog", "x"); // false ?>
Naming and Creating a Variable in PHP The first character of the name must be either a letter or an underscore (_). The remaining characters must comprise only of letters, numbers or underscores.
The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .
if(substr($str, 0,1) == "a" && substr( $str,-1) == "a")
{
// code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With