thepip3r Posted November 2, 2006 Share Posted November 2, 2006 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??? Quote Link to comment Share on other sites More sharing options...
mainewoods Posted November 2, 2006 Share Posted November 2, 2006 [code]<?php if ($condition) { return; }?>[/code]--just do an early return from the function Quote Link to comment Share on other sites More sharing options...
thepip3r Posted November 2, 2006 Author Share Posted November 2, 2006 return is exactly what i was looking for. Thanx mainewoods. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted November 2, 2006 Share Posted November 2, 2006 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]<?phpfunction 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]<?phpfunction 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.