douxi3085 2012-07-03 09:54
浏览 37
已采纳

检查两个字符串是否以相同的字符开头

I'm trying to do this in Object Oriented PHP, but I have an issue when recursion is used (if the first string starts with a "(", I want to check the following char), the other cases work. Here is the code:

public static function different_first($item,$item1) {
    if (substr($item, 0, 1) != substr($item1, 0, 1)) {
        return TRUE;
    } else if (substr($item,0,1)=="(") {
        Object::different_first(substr($item, 1), $item1);
    } else { 
        return FALSE;
    }
}
  • 写回答

2条回答 默认 最新

  • dougang8233 2012-07-03 10:02
    关注

    Missing the return as Mark mentioned. I made a few improvements to your code. This would run a lot faster.

    public static function different_first($item,$item1) {
        if ($item{0} == $item1{0}){
            return false;
        }elseif ($item{0}=="(") {
            return Object::different_first($item{1}, $item1);
        } else { 
            return true;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?