ballhogjoni Posted April 20, 2012 Share Posted April 20, 2012 Hey all, I would like to some how clean up an if statement to be a little cleaner. if(condition && condition && (condition && condition) || (condition && condition) || (condition && condition)) How can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/261286-is-there-a-way-to-clean-up-an-if-statement-with-many-conditions/ Share on other sites More sharing options...
creata.physics Posted April 20, 2012 Share Posted April 20, 2012 you can use the switch function: switch( $if ) { // same thing as if ( $condition == 'value' ) case 'value': do this; break; case 'value2': case 'value3': do that; break; } You can separate your conditions and have it broken down onto separate lines. As my dad always said, "if it ain't broke don't fix it". Quote Link to comment https://forums.phpfreaks.com/topic/261286-is-there-a-way-to-clean-up-an-if-statement-with-many-conditions/#findComment-1338959 Share on other sites More sharing options...
kicken Posted April 20, 2012 Share Posted April 20, 2012 We'd have to see the actual code to know if there is a way to rewrite it using less conditions or a different structure. If your goal is just to reduce the line length you can either a) Use some temp variables to shorten the if -or- b) Separate the conditions onto separate lines. Eg, giving the original statement of: if(condition1 && condition2 && (condition3 && condition4) || (condition5 && condition6) || (condition7 && condition8)) a) $res1 = condition3 && condition4; $res2 = condition5 && condition6; $res3 = condition7 && condition8; if(condition1 && condition2 && $res1 || $res2 || $res3) b) if( condition1 && condition2 && (condition3 && condition4) || (condition5 && condition6) || (condition7 && condition8) ) Quote Link to comment https://forums.phpfreaks.com/topic/261286-is-there-a-way-to-clean-up-an-if-statement-with-many-conditions/#findComment-1338963 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.