-4

I can not understand these steps.

function Palindrome($str) {
    if ((strlen($str) == 1) || (strlen($str) == 0)) {
        echo " THIS IS PALINDROME";
    }
    else {
        if (substr($str,0,1) == substr($str,(strlen($str) - 1),1)) {
            return Palindrome(substr($str,1,strlen($str) -2));
        }
        else { echo " THIS IS NOT A PALINDROME"; }
    }
}

Palindrome("456");
jonhopkins
  • 3,844
  • 3
  • 27
  • 39
user2509218
  • 5
  • 1
  • 1

3 Answers3

3
if ((strlen($str) == 1) || (strlen($str) == 0)) {
    echo " THIS IS PALINDROME";
}

If strlen($str) <= 1 this is obviously a palindrome.

else {
    if (substr($str,0,1) == substr($str,(strlen($str) - 1),1)) {
        return Palindrome(substr($str,1,strlen($str) -2));

     }

If strlen($str) > 1 and if first and last characters of the string are similar, call the same Palindrome function on the inner string (that is the string without its first and last characters).

     else { echo " THIS IS NOT A PALINDROME"; }
}

If first and last characters are not equals, this is not a palindrome.

The principle is to test only the outer characters, and to call the same function again and again on smaller parts of the string, until it has tested every pair of characters that have to be equal if we're dealing with a palindrome.

This is called recursion.

This image illustrates what happens better than my poor english can:

enter image description here

image source

Community
  • 1
  • 1
xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
0

It's recursive...

So it checks the outer and innrer characters. If they match, it continues to the next most outer/inner character, i.e.

NURSESRUN

Will check: Is the first and last char equal? (N=N?) Yes. are the second and second from last equal? (U=U?) - by calling itself again. This is recursion.

If it runs into non equal chars it quits and returns 'NOT A PALINDROME' If it runs out of checks (zero length string for even number of chars, string length 1 for odd numbers) it reaches the 'terminating condition' (no more recursion) and returns 'THIS IS A PALINDROME'

Ryan
  • 3,924
  • 6
  • 46
  • 69
0

Palindrome("456") gets $str == "456". So, looking at branches:

  • if ((strlen($str) == 1) || (strlen($str) == 0)) -> false
  • if (substr($str,0,1) == substr($str,(strlen($str) - 1),1)) is the same as if ("4" == "6")), which is false, so we go to the last branch, outputting that "456" is not a palindrome.

Let's see what would happen for Palindrome("454") gets $str == "456". So, looking at branches:

  • if ((strlen($str) == 1) || (strlen($str) == 0)) -> false
  • if (substr($str,0,1) == substr($str,(strlen($str) - 1),1)) is the same as if ("4" == "4")), which is true, so we call Palindrome(substr($str,1,strlen($str) -2)), which is the same as `Palindrome("5")

Now, inside that function call, we get new variable $str == "5". Performing the same steps, our first if is true, so we echo that it is a palindrome.

For a recursion, it is crucial to remember that each function call has it's own local variables. In other words, when you call Palindrome(...) and inside that function call Palindrome(...) is called again, there are two $str variables in memory, one belonging to the first (outer) call and one to the second (inner) call. Of course, each sees only its own, but once you exit the inner call, you have unchanged $str in the outer call. That's why we had $str == "454" in the first call and $str == "5" in the second. These are named the same, but are two variables existing in the memory (until you exit the second (inner) call of Palindrome()).

Vedran Šego
  • 3,553
  • 3
  • 27
  • 40