user1301428
user1301428

Reputation: 1783

Check if two strings start with the same character

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;
    }
}

Upvotes: 0

Views: 419

Answers (2)

shxfee
shxfee

Reputation: 5306

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;
    }
}

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838096

You are missing a return:

return Object::different_first(substr($item, 1), $item1); 

Upvotes: 4

Related Questions