Palindrome based function using php code

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"); 
-4
source share
3 answers
 if ((strlen($str) == 1) || (strlen($str) == 0)) { echo " THIS IS PALINDROME"; } 

If strlen($str) <= 1 , then 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 the first and last characters of the string are similar, call the same Palindrome function on the internal string (this is a string without its first and last characters).

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

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

The principle is to check only external characters and call the same function again and again on small parts of the string until it checks each pair of characters that should be equal if we are 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

+3
source

Fractal ...

Thus, it checks the external and innrer characters. If they match, it continues the next outer / inner character, i.e.

 NURSESRUN 

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

If it works with unequal characters, it exits and returns 'NOT A PALINDROME' If it runs out of checks (a string of zero length for an even number of characters, a string length of 1 for odd numbers), it reaches the โ€œtermination conditionโ€ (no more recursion ) and returns 'THIS IS A PALINDROME'

0
source

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

  • if ((strlen($str) == 1) || (strlen($str) == 0)) โ†’ false
  • if (substr($str,0,1) == substr($str,(strlen($str) - 1),1)) matches if ("4" == "6")) , which is false, therefore, we move on to the last branch, deducing that "456" is not a palindrome.

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

  • if ((strlen($str) == 1) || (strlen($str) == 0)) โ†’ false
  • if (substr($str,0,1) == substr($str,(strlen($str) - 1),1)) matches if ("4" == "4")) , which is true, therefore we call Palindrome(substr($str,1,strlen($str) -2)) , which is the same as "Palindrome (" 5 ")

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

For recursion, it is important to remember that each function call has its own local variables. In other words, when you call Palindrome(...) and a call to Palindrome(...) is called inside this function, there are two $str variables in memory, one of which refers to the first (external) call and the second (internal) call Of course, everyone sees only his own, but as soon as you exit the internal call, $str unchanged in the external call. That is why we had $str == "454" in the first call and $str == "5" in the second. They are called the same, but there are two variables that exist in memory (until you exit the second (internal) Palindrome() call).

0
source

Source: https://habr.com/ru/post/1487477/


All Articles