jeudi 30 novembre 2017

isset() returning nothing for NULL

I have written this php code

//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);
        }
    }

Here $request['advertisername']=null coming from fronend. So this condition will be true if (!isset($request['advertisername'])) {. But unfortunately it's always going to last else block.

If i save $request['advertisername'] in a variable like this. Then this condition if (!isset($advertiserName)) { becomes true.

//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);
    }
}

I checked the condition via var_dump(). Anyone can explain why it is happening?

Aucun commentaire:

Enregistrer un commentaire