How to Read Whether a Checkbox is Checked in PHP

In this tutorial, you can find comprehensive information on how to read whether a checkbox is checked in PHP.

Here, we will demonstrate two handy functions that will assist you in reading whether a checkbox is checked in PHP. Those functions are isset() and empty().

Watch a course Learn object oriented PHP

Applying the isset() Function

This is an inbuilt function that is capable of checking whether a variable is set.

With the isset() function, you can also check whether an array, a declared variable, or an array key is null. The isset() function returns false if it does and true in all the possible situations.

The syntax of the isset() function looks like this:

isset(mixed $var, mixed ...$vars): bool

Now, let’s see the code that will check whether a checkbox is checked:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>W3docs Example</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <style>
      .gfg {
        font-size: 40px;
        font-weight: bold;
        color: green;
      }
      body {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <?php
      if(isset($_GET['submit'])) { 
        $var = isset($_GET['option1']); 
	    if($var) { 
	      echo "Option 1 submitted successfully"; 
        } 
      } 
    ?>
    <div class="container">
      <div class="gfg">W3docs</div>
      <h2>Form control: checkbox</h2>
      <p>The form below contains one checkbox.</p>
      <form method="get" action="/form/submit">
        <div class="checkbox">
          <label> <input type="checkbox" name="option1" value="Option 1" />Option 1 </label>
          <button name="submit" value="true">SUBMIT</button>
        </div>
      </form>
    </div>
  </body>
</html>

The output of this code will show that option 1 is successfully submitted.

Applying the empty() Function

In this section, we will illustrate another inbuilt function: the empty() function.

With empty(), you can test whether a variable is empty or not.

The syntax is as follows:

empty(mixed $var): bool

Let’s see how to use this function for reading if a checkbox is checked:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>W3docs</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
      .gfg {
        font-size: 40px;
        font-weight: bold;
        color: green;
      }
      body {
        text-align: center;
      }
    </style>
  </head>
  <body>
    <?php
      if(!empty($_GET['submit'])) { 
      $var = empty($_GET['option1']) ? null : $_GET['option1'] ; 
      if($var){ 
        echo "Option 1 submitted successfully"; 
      } 
     } 
    ?>
    <div class="container">
      <div class="gfg">W3docs</div>
      <h2>Form control: checkbox</h2>
      <p>The form below contains one checkbox.</p>
      <form method="get" action="/form/submit">
        <div class="checkbox">
          <label> <input type="checkbox" name="option1" value="Option 1" />Option 1 </label>
          <button name="submit" value="true">SUBMIT</button>
        </div>
      </form>
    </div>
  </body>
</html>

The output will show that option 1 is successfully submitted.