I have the following multiple checkbox selection:
<input type="checkbox" name="fruit_list[]" value="apple">Apple
<input type="checkbox" name="fruit_list[]" value="banana">Banana
<input type="checkbox" name="fruit_list[]" value="mango">Mango
<input type="checkbox" name="fruit_list[]" value="orange">Orange
form connects to processor.php
via POST method. Validation:
if ( empty($_POST['fruit_list']) ){
echo "You must select at least one fruit.<br>";
} else{
foreach ( $_POST['fruit_list'] as $frname ){
echo "Favourite fruit: $frname<br>";
}
}
My Questions (above code works! But unclear points for me):
If I don't select any of checkboxes and then submit form, does
$_POST
array contain an index called$_POST['fruit_list']
?Assuming your answer "No", then how is it possible to use
empty()
to that non existed array element? Non-existed array element meansNULL
?What is the difference using
!isset($_POST['fruit_list'])
instead ofempty()
I understand the difference between empty()
and isset()
generally.
Can you explain in this context of example?