#1
  1. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,122
    Rep Power
    539

    Start function over again with new values


    Consider the following script. From within myFunction, I wish to check some condition, and if met, cancel the current execution of myFunction and initiate new execution of myFunction with different arguments. What is the best way to go about doing so? Thank you

    PHP Code:
    function myFunction($x,$y,$z){
        echo(
    'My Function start<br>');
        if((
    $x+$y+$z)==6) {
            
    $x=$x+1;
            echo(
    'Update some values and start this function over<br>');
            
    myFunction($x,$y,$z);
            die;
            
    //header("LOCATION: {$_SERVER['PHP_SELF']}?x={$x}&y={$y}&z={$z}");
            //die();
        
    }
        echo(
    'My Function end<br>');
        return 
    $x+$y+$z;
    }
      echo(
    'My Main Script<br>');
      
    $x=isset($_GET['x'])?$_GET['x']:1;
      
    $y=isset($_GET['y'])?$_GET['y']:2;
      
    $z=isset($_GET['z'])?$_GET['z']:3;
      
    $sum=myFunction($x,$y,$z);
      die(
    "The value is {$sum}. My Main Script is complete<br>"); 
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,884
    Rep Power
    6356
    Why not just:

    PHP Code:
    function myFunction($x,$y,$z){ 
        echo(
    'My Function start<br>'); 
        if((
    $x+$y+$z)==6) { 
            
    $x++; 
            echo(
    'Update some values and start this function over<br>'); 
            return 
    myFunction($x,$y,$z); 
        } 
        echo(
    'My Function end<br>'); 
        return 
    $x+$y+$z

    Since this is obviously a simplified example, you have to note that it's possible for functions to recurse infinitely unless you add controls into your code to limit the number of recursion attempts. Not this particular code I just posted, that will only call itself once, but recursive functions (functions which call themselves) could have dangerous memory repercussions if they aren't controlled.

    Comments on this post

    • NotionCommotion agrees : Thank you
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,122
    Rep Power
    539
    Originally Posted by ManiacDan
    Why not just:
    Why not indeed? Just got myself stuck thinking one way which blinded me from the obvious.
  6. #4
  7. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,201
    Rep Power
    2012
    Originally Posted by ManiacDan
    Not this particular code I just posted, that will only call itself once, ...
    Can't say if I have missed something, but where in the code do you specify it will only "call itself once"?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,122
    Rep Power
    539
    Originally Posted by MrFujin
    Can't say if I have missed something, but where in the code do you specify it will only "call itself once"?
    PHP Code:
    if(($x+$y+$z)==6) { 
            
    $x++; 
  10. #6
  11. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,201
    Rep Power
    2012
    Now I see where I was wrong.

    I first saw it as it would keep running until the sum is 6. But it is of course the opposite (only run if sum is 6).
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,884
    Rep Power
    6356
    Right, but since this is obviously an example function I had to include the disclaimer. This particular example cannot run more than once, but in general recursion is dangerous when the memory space can expand infinitely.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo