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.
Unchecked checkboxes do not send any data to the server, meaning their corresponding array key will be absent rather than empty. This tutorial explains 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 <kbd class="highlighted">isset()</kbd> and <kbd class="highlighted">empty()</kbd>.
Applying the isset() Function
This is an inbuilt function that is capable of checking whether a variable is set.
With the <kbd class="highlighted">isset()</kbd> function, you can also check whether an array, a declared variable, or an array key is null. The <kbd class="highlighted">isset()</kbd> function returns false if the variable is not set or is null, and true otherwise.
The syntax of the <kbd class="highlighted">isset()</kbd> function looks like this:
php isset function syntax
isset(mixed $var, mixed ...$vars): boolNow, let’s see the code that will check whether a checkbox is checked:
read if a checkbox is checked with php isset()
<!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>
<input type="submit" name="submit" value="SUBMIT" />
</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 <kbd class="highlighted">empty()</kbd> function.
With <kbd class="highlighted">empty()</kbd>, you can test whether a variable is empty or not. However, for checkboxes, <kbd class="highlighted">isset()</kbd> is the standard and safest approach in modern PHP, as it avoids warnings when the key is absent. Note that empty() returns true if the key is missing or evaluates to a falsy value. Since unchecked checkboxes do not send data, empty() is generally less reliable than isset() for this specific use case.
The syntax is as follows:
php empty() function syntax
empty(mixed $var): boolLet’s see how to use this function for reading if a checkbox is checked:
php read if a checkbox is checked with empty
<!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 (isset($_GET['submit']) && !empty($_GET['option1'])) {
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>
<input type="submit" name="submit" value="SUBMIT" />
</div>
</form>
</div>
</body>
</html>The output will show that option 1 is successfully submitted.