Jump to content

If, Else control flow


alec

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.