Jump to content

Is there a way to exit a function's processing without halting script execution?


thepip3r

Recommended Posts

I know I could do this easily with checking a condition and then having the rest of the code only execute if that condition was true or false but I just wanted to know if there was a PHP equivalent to the VB "exit function" where it just skips code execution for that scope of code and then returns to processing like usual?? maybe something like a PHP loop continue but for functions???
Link to comment
Share on other sites

I'll toss in that there's at least two philosophies that I know of when it comes to returning early from a function.

One side will tell you to only return a single time at the very end.
[code]<?php
function SingleReturn(){
  $Ret = NULL; // Our return value
  if(!$no_error_check_1){
    // Our first test for an error showed no error, so we can continue
    // some statements
    if(!$no_error_check_2){
      // Our second test for an error passed so we keep on going
      // some more statements
      $Ret = $SomeFinalValue;
    }
  }
  return $Ret;
}
?>[/code]

The other side will tell you to return whenever the function can no longer continue.
[code]<?php
function ManyReturns(){
  if($error1){
    return NULL;
  }

  if($error2){
    return NULL;
  }
  // Now we're done with errors we can continue
  // some stuff
  return $SomeFinalValue;
}
?>[/code]

People who favor the first method will argue that it's better to have one and only return simply because you know that all of the code in the function is looked at.  I.E. with early returns you run the risk of forgetting to do something integral to your application.  Also, as you indent further and further you will be encourage to break your functions up into multiple functions which can help maintain readability.

People who favor the second method will argue that the structure of the function is easier to follow because you know that once you reach a certain point you're free to continue processing.  It also requires less indenting so can also be easier to read.

I myself favor the first method.  If I find myself indenting more than 3 or 4 levels in I usually create another function and it helps down the road when I return to my read my old code.

It doesn't matter which one you choose though.  Just pick one and use it consistently throughout your project.  If you mix them up you'll find yourself having a harder time debugging because you may not notice an early return in one function when you thought it returned only once at the end.

Hope that helps a bit.
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.