So, I wanted to avoid an
if (...) { }
else if (...) { }
else { }
scenario to avoid duplicating code. After a little playing, I got the following working (works from the testing i've done). Am wondering if anyone has any feedback on this approach or suggestions on on possible improvement?
Sometimes I think I know what I'm doing, sometimes I don't.
<?php
// define("MEMBER_LIMIT",0); // no member limit
defined("MEMBER_LIMIT",100); // limit to 100 ppl in group
// $memcount is set via sn SQL query
$memcount = 9; // number of people currently in group (let's say).
if (!defined("MEMBER_LIMIT") ||
((defined("MEMBER_LIMIT") && MEMBER_LIMIT == 0) ||
(MEMBER_LIMIT > 0 && $memcount>0 && $memcount < MEMBER_LIMIT))) {
// will do something
}
else {
// will do something else.
}
?>
1 Answer 1
How about:
if (!defined("MEMBER_LIMIT") || MEMBER_LIMIT == 0 || $memcount < MEMBER_LIMIT) {
// will do something
}
else {
// will do something else.
}
The key is to look for both complementary (multiple items that accomplish the same thing by different means) and contradicting criteria (which would interrupt your desired flow of logic) in if statements
-
\$\begingroup\$ If you look into the code,
MEMBER_LIMIT
is always defined. \$\endgroup\$hakre– hakre2011年10月10日 09:58:35 +00:00Commented Oct 10, 2011 at 9:58 -
\$\begingroup\$ Indeed, I thought the user may comment back in the initial line however \$\endgroup\$Ergo Summary– Ergo Summary2011年10月10日 09:59:25 +00:00Commented Oct 10, 2011 at 9:59
-
\$\begingroup\$ yes, if i leave 'defined(...)' in there. there are plans to have certain things defined and others not, which i was the !defined(...) check. \$\endgroup\$coelmay– coelmay2011年10月10日 10:09:41 +00:00Commented Oct 10, 2011 at 10:09