tbode2021 Posted May 15, 2021 Share Posted May 15, 2021 <input class="form-check-input" type="checkbox" id="a201" name="a201" value="a"> <label class="form-check-label" for="a201">Acetaminophen</label> $score = (isset($_POST['a201']) && $_POST['a201']=='a')? 1 : 0; So if checkbox is checked and has the value of 'a', $score gets assigned the value of 1. Otherwise $score gets assigned the value of 0? Quote Link to comment https://forums.phpfreaks.com/topic/312708-is-this-the-syntax-for-an-if-then-else/ Share on other sites More sharing options...
maxxd Posted May 16, 2021 Share Posted May 16, 2021 It's a shorthand for if:else called a ternary operator. You'll have to scroll down the link for the full explanation as apparently there's not a name link there. Your reading of the line is correct, yes - a checkbox variable will only exist in $_POST when it's checked; so if it's checked and equal to 'a' $score will be 1, otherwise $score will be 0. Quote Link to comment https://forums.phpfreaks.com/topic/312708-is-this-the-syntax-for-an-if-then-else/#findComment-1586573 Share on other sites More sharing options...
Barand Posted May 16, 2021 Share Posted May 16, 2021 Another way is to give the checkbox form input the value you actually want when it is set (ie "1") <input class="form-check-input" type="checkbox" id="a201" name="a201" value="1"> then $score = $_POST['a201'] ?? 0; which means "if the checkbox exists and is not null, use its value, otherwise default to "0". (See Null Coalesce operator) Quote Link to comment https://forums.phpfreaks.com/topic/312708-is-this-the-syntax-for-an-if-then-else/#findComment-1586575 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.