PHP POST array with empty and isset -


i have 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 processor.php via post method. validation:

if ( empty($_post['fruit_list']) ){     echo "you must select @ least 1 fruit.<br>";  } else{     foreach ( $_post['fruit_list'] $frname ){         echo "favourite fruit: $frname<br>";     } } 

my questions (above code works! unclear points me):

  1. if don't select of checkboxes , submit form, $_post array contain index called $_post['fruit_list'] ?

  2. assuming answer "no", how possible use empty() non existed array element? non-existed array element means null ?

  3. what difference using !isset($_post['fruit_list']) instead of empty()

i understand difference between empty() , isset() generally.

can explain in context of example?

  1. if don't select of checkboxes , submit form, $_post array contain index called $_post['fruit_list']

no, key fruit_list not exist

to check key exists in array better use array_key_exists because if have null values isset returns false

but @ case isset way

isset - determine if variable set , not null (have value).

empty - determine whether variable empty (0, null, '', false, array()) can't understand 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) 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -