代码之家  ›  专栏  ›  技术社区  ›  RAUSHAN KUMAR

isset()为NULL不返回任何内容

  •  0
  • RAUSHAN KUMAR  · 技术社区  · 8 年前

    我已经写了这个php代码

    //check if user has active plan and search keyword is not empty
        if (!empty($request['advertisername']) && ($userSubscriptionType == env('AMEMBER_STD_PLAN_CODE') || $userSubscriptionType == env('AMEMBER_PRE_PLAN_CODE'))) {
            $advertisername = 'LOWER(post_owner) like "%' . strtolower($request["advertisername"]) . '%"';
        } else {
            //if search keyword is null, means page is loaded for first time
            if (!isset($request['advertisername'])) {
                $advertisername = "true";
            } else {//if search keyword is not null, and user has not active plan
                $response->code = 400;
                $response->msg = "Permission issues";
                $response->data = "";
                return json_encode($response);
            }
        }
    

    在这里 $request['advertisername']=null 来自弗罗宁。所以这个条件是真的 if (!isset($request['advertisername'])) { . 但不幸的是,它总是会持续一个街区。

    如果我保存 $request['advertisername'] 在这样的变量中。那么这个条件 if (!isset($advertiserName)) { 成为现实。

    //check if user has active plan and search keyword is not empty
    if (!empty($request['advertisername']) && ($userSubscriptionType == env('AMEMBER_STD_PLAN_CODE') || $userSubscriptionType == env('AMEMBER_PRE_PLAN_CODE'))) {
        $advertisername = 'LOWER(post_owner) like "%' . strtolower($request["advertisername"]) . '%"';
    } else {
        //if search keyword is null, means page is loaded for first time
        $advertiserName=$request['advertisername'];
        if (!isset($advertiserName)) {
            $advertisername = "true";
        } else {//if search keyword is not null, and user has not active plan
            $response->code = 400;
            $response->msg = "Permission issues";
            $response->data = "";
            return json_encode($response);
        }
    }
    

    我通过var_dump()检查了条件。任何人都能解释为什么会这样?

    3 回复  |  直到 8 年前
        1
  •  1
  •   Mehdi Alipour    8 年前

    is_null() 而不是 !isset() .

        2
  •  1
  •   kmdm    8 年前

    您可以改用array\u key\u exists():

    if (!array_key_exists('advertisername', $request)) {
    

    请注意,iSet()的PHP手册中说:

    isset — Determine if a variable is set and is not NULL
    
        3
  •  1
  •   Gopal Panadi    8 年前

    一个好方法是检查变量是否为空,因为它同时检查isset和null

    eg if(!empty($your_variable) {    /** your code here **/   }
    
    推荐文章