when a function in itself...

Discussion in 'PHP' started by fengfeng, Oct 1, 2007.

  1. #1
    the first,i write a small program like this

    <?php
    function a(){
    echo "test";
    a();
    }
    a();
    ?>
    PHP:
    i think my computer(yes,i run this script in my local computer)would be too busy to finish it.but im wrong,the browse auto disconnect.why the program would not go on??
    thank you.
     
    fengfeng, Oct 1, 2007 IP
  2. undir

    undir Peon

    Messages:
    696
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    its a recursive loop
     
    undir, Oct 1, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    As stated above, the code would never finish running because you're calling the same function in the function itself. With a little bit of logic, you'll why it never ends.

    If you just want to call it once within itself, use a static variable inside it which counts the times it's been executed, and only call it again if this certain amount you specify hasn't been passed.
     
    nico_swd, Oct 1, 2007 IP
  4. undir

    undir Peon

    Messages:
    696
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    by the way. what are you trying to achieve?
     
    undir, Oct 1, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    I think he's just learning PHP and messing around. :)
     
    nico_swd, Oct 1, 2007 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think he's actually asking "this should go on forever but it doesn't... why not?"

    I could be wrong but that's the way I read it and OP the answer is because PHP has a max_execution_time setting that is designed to stop exactly this type of script.

    You can always try and override it with a call to set_time_limit(0);
     
    TwistMyArm, Oct 1, 2007 IP
  7. fengfeng

    fengfeng Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hehe,ur right.im new in PHP.
    in face,when i run this scirpt in my computer,the browse would disconnect automatic.its very interesting.i think there is a system in php,which can stop this recursive loop.And i try to use some php script to make my computer crazy.:D
     
    fengfeng, Oct 1, 2007 IP