在php中检查字符串的第一个和最后一个字符最简单的方法是什么

在php中检查字符串的第一个和最后一个字符最简单的方法是什么,php,string,Php,String,检查字符串$str的第一个字母是否为'a',最后一个字母是否也是'a'的最简单方法是什么?或使用substr获取最后一个字符: if($str[0] == 'a' && $str[strlen($str) - 1] == 'a') { //do whatever you wanted } if($str[0] == 'a' && substr($str,-1) == 'a') { //do whatever you wanted } North

检查字符串
$str
的第一个字母是否为
'a'
,最后一个字母是否也是
'a'
的最简单方法是什么?

或使用substr获取最后一个字符:

if($str[0] == 'a' && $str[strlen($str) - 1] == 'a') {
    //do whatever you wanted
}
if($str[0] == 'a' && substr($str,-1) == 'a') {
    //do whatever you wanted
}

North Creative:您的意思是
substr(
),而不是
$str.substr(
)。您可能会发现,在中可以找到,并且很有帮助。在PHP7.1中,您甚至可以这样做:
if(
if($str[0] == 'a' && substr($str,-1) == 'a') {
    //do whatever you wanted
}