PHP POST array with empty and isset

后端 未结 3 1259
灰色年华
灰色年华 2021-01-15 11:26

I have the following multiple checkbox selection:

Apple


        
3条回答
  •  半阙折子戏
    2021-01-15 11:49

    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)
    

提交回复
热议问题