Jump to content

can you make a function repeat?


php_joe

Recommended Posts

is there a command that will abort a function and rewind it back to the beginning?

 

Something along the lines of this:

 

<?
if($handle = fopen($url)){
fwrite($handle, $info);
fclose($handle);
}else{

// here is where I want it to return to the beginning of the function

}
?>

Link to comment
Share on other sites

Seems an odd request given your example, but recursion may be what your after.

 

<?php

function foo() {
  if($handle = fopen($url)){
    fwrite($handle, $info);
    fclose($handle);
  } else {
    foo();
  }
}

foo();

?>

Link to comment
Share on other sites

Id'e be carefull though... the example I gave would simply run until your server does. It would create an infinant recursion.

 

There's a way to prevent that though, right? so that the code stops parsing when the page is closed?

Link to comment
Share on other sites

There's a way to prevent that though, right? so that the code stops parsing when the page is closed?

 

When using recursion you must always define a (set of) base case(s).  Defining attainable base cases ensure that your program will terminate.  To illistrate this, consider a recursively defined power function, we'll call it rpow().  To begin we must know something about abour problem; in this case, that x to the 0th power is 1 and that x to kth power is x^(k-1) * x (remember your algebra? ;)) or rpow(x, k-1) * x.  With this information we can construct a recursive solution.

 

<?php
rpow($x, $k){
  // Our base case is x^0 = 1.  We know that this case will always be reached, 
  // provided that k is an integer greater than -1 because in our recursive step 
  // below we are always subtracting 1 from k, it follows that k will eventually be 0.
  if($x == 0){
    return 1;
  }
  // If k != 0 then, by the definition we provided (x^(k-1) * k) we will recurse (call "ourself)
  return rpow($x, $k - 1) * $x;
}
?>

 

Recursion provides an elegant solution to many problems, however, as the previous posters have said  we would need to know more about the problem you are trying to solve in order to help you along with a solution.

 

Best,

 

Patrick

Link to comment
Share on other sites

You can do what thorpe said:

 

<?php

function foo() {
  if($handle = fopen($url)){
    fwrite($handle, $info);
    fclose($handle);
  } else {
    foo();
  }
}

foo();

?>

 

but u can add a couple lines of code.

 

<?php
$i = 0;
function foo() {
if($i == 5) {
  if($handle = fopen($url)){
    fwrite($handle, $info);
    fclose($handle);
    $i++;
  } 
  }
  else {
    foo();
    $i++;
  }
}

foo();

?>

Link to comment
Share on other sites

I think we might need a bit more information about what you're trying to achieve in order to help you with your function.

 

Nothing specific at the moment, but I've occationally run into a situation that I'd want to repeat, maybe with a slightly altered set of circumstances.

 

If you had a situation where the function was in an endless loop (like, you'd set the timeout at 0) would it continue to loop if the visitor closed the webpage?

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.