Jump to content

Recursion problem in PHP5


moby04

Recommended Posts

I've got a problem with recursion function in PHP. The code I've written does nothing and ofcourse I know how to fix it but I have found a certain problem. The code is:

 

<?php 
function reverse($string)
{
    if(strlen($string)>0)
        reverse($string); //should be: reverse(substr($string, 1));
    echo substr($string, 0, 1);
    return;
}
reverse("Hello");
?>

 

When I try to run it from localhost (I use Apache2 na Ubuntu 7.04 z PHP 5.2.1, script run at Firefox 2.0.0.5) my browser displays file download form insted of HTML result. At the begining I thought it is the problem with the neverending recursion but when I tried with the following code everything was fine (it breaked at value of 18271):

 

<?php 
function rekurencja()
{
    static $i = 1;
    echo $i++."<br />\n";
    rekurencja();
    return;
}    
rekurencja("Hello");
?>

 

I asked already at polish forum but I had no answer (rekurencja is just a recursion :) ). Can you tell me what is the problem?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.