如果选择多个,价格会上涨

I'm working on a project. My codes are simply written below

HTML

<input type="checkbox" name="A" value="A">A
<input type="checkbox" name="B" value="B">B
<input type="checkbox" name="C" value="C">C
<input type="submit" name="submit" value="Submit">

PHP

<?php
$price=0;
  if(isset($_POST["submit"])){
  //the code goes here
  }
?>

If only one of the option is chosen, it's free (no price). But, if the user chooses more than one, the $price is +10 in every option. So, it can be illustrated like this

  • Choose 1 = free
  • Choose 2 = +10
  • Choose 3 = +20

I have no idea with my PHP and the line //the code goes here is still empty. Any idea?

Homework, huh? I didn't even have to go to college for that one.

<?php

if( isset($_POST) )
{
  $count = 0;
  $arr = [
    array_key_exists('A', $_POST),
    array_key_exists('B', $_POST),
    array_key_exists('C', $_POST)
  ];

  for( $i = 0; $i < 3; $i++ ) {
    if( $arr[$i] ) $count++;
  }
  // Now count the total - 1 and * it by 10
  if( $count > 1 ) $total = ($count - 1) * 10;
}

Basically this script will check for the key in the $_POST array, if you do not select a checkbox it would not create the key for _POST array and thus it would be false.

If it's false then just skip by default. However, if it's true, it will increment to the $count variable. Then if $count is greater than one than we add create a new variable of $total = $count - 1. That will remove one value from $count then times it by 10.