0

With this code:

function lookForTheA($str, $pos) {
    if ($str[$pos] == 'A') {
        return 'is in position: '.$pos;
    } else {
        $pos++;
        return lookForTheA($str, $pos);
    }
}

$str = 'BBBBBBBBBBBBBBBBBBBBBBA';
$res = lookForTheA($str, 0);
echo $res;

I need to know if the second return (line 6) is needed. If it's a good way to resolve it.

  • I'd rather have one return in line 3 and in this line just function call without return. – cn007b Sep 13 '17 at 07:07
  • why not just removing the return and see how it goes ? – Calimero Sep 13 '17 at 07:35
  • i think you should remove the second return, add a test if the $pos in your $str exist instead of just else and return null if it out of range – Frankich Sep 13 '17 at 07:36
  • I think it's not duplicate, The other question explain the recursion and it resolve one of the points, but I want to know if this is a good practice or I can do it in another way too. And yes, I try to remove the second and it doesn't works but I didn't understand well why –  Sep 13 '17 at 07:50
  • Asking for good practise is off-topic on StackOverflow. – Veve Sep 13 '17 at 07:58

0 Answers0