Getting confused with empty, isset, !empty, !isset

后端 未结 6 414
感情败类
感情败类 2021-01-21 19:10

I have the following which doesn\'t work properly as $_GET[\'category\'] can also equal 0.

if ( empty( $_GET[\'category\'] ) ){
    // do something
         


        
6条回答
  •  一个人的身影
    2021-01-21 19:36

    This isn't really hard to do: isset is the method you need.

    if (!isset($_GET['category'])) {
        // category isn't set
    } elseif ($_GET['category'] === '0') {
        // category is set to 0
    } else {
        // category is set to something other than 0
    }
    

    Note that I have compared for exact equality to the string '0', because GET and POST variables are always strings (or occasionally arrays) and never numbers when PHP first receives them.

提交回复
热议问题