Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Palindrome("456")</code> gets <code>$str == "456"</code>. So, looking at branches:</p> <ul> <li><code>if ((strlen($str) == 1) || (strlen($str) == 0))</code> -> false</li> <li><code>if (substr($str,0,1) == substr($str,(strlen($str) - 1),1))</code> is the same as <code>if ("4" == "6"))</code>, which is false, so we go to the last branch, outputting that "456" is not a palindrome.</li> </ul> <p>Let's see what would happen for <code>Palindrome("454")</code> gets <code>$str == "456"</code>. So, looking at branches:</p> <ul> <li><code>if ((strlen($str) == 1) || (strlen($str) == 0))</code> -> false</li> <li><code>if (substr($str,0,1) == substr($str,(strlen($str) - 1),1))</code> is the same as <code>if ("4" == "4"))</code>, which is true, so we call <code>Palindrome(substr($str,1,strlen($str) -2))</code>, which is the same as `Palindrome("5")</li> </ul> <p>Now, inside that function call, we get <strong>new</strong> variable <code>$str == "5"</code>. Performing the same steps, our first <code>if</code> is true, so we echo that it is a palindrome.</p> <p>For a recursion, it is crucial to remember that each function call has it's own local variables. In other words, when you call <code>Palindrome(...)</code> and inside that function call <code>Palindrome(...)</code> is called again, there are two <code>$str</code> 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 <strong>unchanged</strong> <code>$str</code> in the outer call. That's why we had <code>$str == "454"</code> in the first call and <code>$str == "5"</code> in the second. These are named the same, but are two variables existing in the memory (until you exit the second (inner) call of <code>Palindrome()</code>).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload