2

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):

  1. If I don't select any of checkboxes and then submit form, does $_POST array contain an index called $_POST['fruit_list'] ?

  2. Assuming your answer "No", then how is it possible to use empty() to that non existed array element? Non-existed array element means NULL ?

  3. What is the difference using !isset($_POST['fruit_list']) instead of empty()

I understand the difference between empty() and isset() generally.

Can you explain in this context of example?

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
Amali
  • 63
  • 1
  • 8

3 Answers3

1
  1. If I don't select any of checkboxes and then submit form, does $_POST array contain an index called $_POST['fruit_list']

No, key fruit_list does not exist

To check if key exists in array better use array_key_exists because if you have NULL values isset returns false

But in your case isset is a good way

isset - Determine if a variable is set and is not NULL (have any value).

empty - Determine whether a variable is empty (0, null, '', false, array()) but you can't understand if variable or key exists or not

For example:

$_POST['test'] = 0;
print 'isset check: ';
var_dump(isset($_POST['test']));
print 'empty check: ';
var_dump(empty($_POST['test']));

$_POST['test'] = null;

print 'isset NULL check: ';
var_dump(isset($_POST['test']));

print 'key exists NULL check: ';
var_dump(array_key_exists('test', $_POST));

isset check: bool(true)
empty check: bool(true)
isset NULL check: bool(false)
key exists NULL check: bool(true)
jjanko3
  • 124
  • 1
  • 13
Dolbik
  • 126
  • 4
0

use print_r() to print the post data..

<?php 
echo "<pre>"; print_r($_POST);
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>";
    }
}
?>

If you do not select any checkbox then you will not get $_POST['fruit_list'], array index fruit_list does not exist in array

for difference between isset() and empty() Visit Here

Community
  • 1
  • 1
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
0

Answer:

1. $_POST array does not contain $_POST['fruit_list']
2. First answer is "No".A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.see in php.net
3. Empty checks if the variable is set and if it is it checks it for null, "", 0, etc. Isset just checks if is it set, it could be anything not null. see

Community
  • 1
  • 1
Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29