Jump to content

Recommended Posts

<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?

Link to comment
https://forums.phpfreaks.com/topic/312708-is-this-the-syntax-for-an-if-then-else/
Share on other sites

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.

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)

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.