alec Posted July 14, 2009 Share Posted July 14, 2009 At the moment i have the following structure in some code. Of course this is simplified, but you should get the gist. Basically i want to do x if ($a && complexandtimeconsumingfunction()), otherwise do y. However, i only want to run the function if ($a), so my question is is there another way to achieve this without duplicating the code that is 'do x;' in the example (which is actually quite lengthy) and without the overhead of a function call. if ($a) { if (complexandtimeconsumingfunction()) { do x; } else { do y; } } else { do y; } Thanks all, any help's appreciated Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted July 15, 2009 Share Posted July 15, 2009 if ($a && complexandtimeconsumingfunction()) { do x; } "x" will not fire unless both $a AND complexandtimeconsumingfunction() are/return true ... precluding the fact that $a is true ... which if I understand your post correctly, is what you want? so .... if ($a && complexandtimeconsumingfunction()) { do x; } else { do y; } Or am I missing something? Quote Link to comment Share on other sites More sharing options...
alec Posted July 15, 2009 Author Share Posted July 15, 2009 You are i'm afraid, sorry i didn't explain it too well but i accidentally posted when i'd half written the post and had to quickly edit it to make sense. i only want the function to run if $a returns true, since the function is quite time consuming, and most of the time $a returns false anyway. i thought of something like this when posting: switch(!$a){ case false: if (complexfunction()) { do x; break; } default: do y; } Will that work how i think it will, and are there any better ways of doing this? Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted July 15, 2009 Share Posted July 15, 2009 The first way you had it is a simple as logic would allow. The other option is to redesign the way the whole thing is working. For instance, if $a is a GLOBAL then could you put code inside of "do y;" that will determine if $a is false and if $a is true it will just return null. Then you can execute "do y;" at a more 'code convenient' time. That would at least take the "do y;" argument out of the original condition. Depending on how your script works and what the functions do that may not work though. Regardless, look at your script through different lenses like that and you may figure out different ways of doing it. Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted July 15, 2009 Share Posted July 15, 2009 The problem with your switch statement is that if complexfunction() is false, do y; won't execute. if ($a && complexandtimeconsumingfunction()) { do x; } else { do y; } This will work because if $a is false, it will not continue with the second part of the conditional. 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.