/    Sign up×
Community /Pin to ProfileBookmark

Simplified If Statement

I’ve got a form where I’m doing something based on if two fields were filled out. I’ll refer to these fields as FieldA and FieldB.

What I’m need to do is run a code if both are filled out, another if FieldA is filled only and a third if FieldB is fillled out. If neither are filled out do nothing.

I’m thinking of the following.

I’ve not tested this, but is the best way to do it?

[code]
if (!empty($_POST[‘FieldA’]) && !empty($_POST[‘FieldB’])){
// Both Filled Out
} elseif (!empty($_POST[‘FieldA’]) && empty($_POST[‘FieldB’])){
/// Only Field A Filled Out
} elseif (empty($_POST[‘FieldA’]) && !empty($_POST[‘FieldB’])){
// Only Field B filled out
}
[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@sibertNov 01.2021 — I have no knowledge of PHP, but basically all language have a switch function, which normally is cleaner and more readable than many nested if > else. Regardless of language.

https://www.php.net/manual/en/control-structures.switch.php
Copy linkTweet thisAlerts:
@ginerjmNov 01.2021 — Looks good to me - and I do write in PHP
Copy linkTweet thisAlerts:
@NogDogNov 01.2021 — switch gets a bit ungainly in this situation, as we're not just creating cases based on a single value, so it might be best to stick with what you have (very subjective, though). I suppose you could do something like this, but not sure it's an improvement maintenance-wise?
[code=php]
switch(post_a_or_b('FieldA', 'FieldB')) {
case 'both':
// do stuff with both
break;
case 'a':
// do stuff with a;
break;
case 'b':
// do stuff with b;
break;
case 'neither':
// nothing to do?
break;
default:
// throw Exception???
}

function post_a_or_b($a, $b) {
$cases = ['neither', 'b', 'a', 'both'];
return($cases[2 * !empty($_POST[$a]) + !empty($_POST[$b])]);
}
[/code]

PS: Untested!
Copy linkTweet thisAlerts:
@ginerjmNov 01.2021 — Stick with the If that you posted. The post by NogDog is difficult to understand for me and I do write PHP.

Yes - an ifelse stucture that goes on and on can be a problem and perhaps hard to follow but in your case it is fairly short and sweet and clear.
Copy linkTweet thisAlerts:
@SempervivumNov 01.2021 — My contribution :) :
``<i>
</i>$filledA = !empty($_POST['FieldA']);
$filledB = !empty($_POST['FieldB']);
switch (true) {
case $filledA &amp;&amp; $filledB:
echo 'both';
break;
case $filledA &amp;&amp; !$filledB:
echo 'A only';
break;
case !$filledA &amp;&amp; $filledB:
echo 'B only';
break;
case !$filledA &amp;&amp; !$filledB:
echo 'none';
break;
}<i>
</i>
`</CODE>
It works, however I do not recommend to use it as it's tricky coding and breaks the semantics of <C>
switch`.
Copy linkTweet thisAlerts:
@NogDogNov 01.2021 — And there's always the possibility (by no means a sure thing) that it's a "code smell" that might be avoided by thinking about why you're doing this, and if there's a better way than 3 different operations depending on which combination of things is set. It may, in fact, be the "best" way; but not knowing what's actually going on in detail, I can't be positive. :)
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...