Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

felicia00's avatar

is this the correct way to use array & isset in php?

$checkuser = getUser();
        $userrole=array($checkuser);

        if(isset($userrole))
        {
        $userrole= explode(",", $checkuser ->GetMultiRole);
        }
0 likes
2 replies
rodrigo.pedra's avatar

array($checkuser) will wrap the $checuser variable in an array.

isset(...) returns true if the variable exists and is not null

reference: https://www.php.net/manual/en/function.isset.php

So the condition if(isset($userrole)) will always evaluate to true.

I guess you might be after this:

$checkuser = getUser();

if(! empty($checkuser)) {
    $userrole = explode(",", $checkuser->GetMultiRole);
} else {
    $userrole = array();
    // or using the short-array syntax
    // $userrole = [];
}

empty() will return true when a variable is empty. To understand what conditions the empty(...) function considers a variable to be empty, please read this reference:

https://www.php.net/manual/en/function.empty.php

As you are using Laravel (you marked the post in the Laravel category) you could use the filled(...) helper, which will check if the variable is "filled" with a value:

$checkuser = getUser();

if(filled($checkuser)) {
    $userrole = explode(",", $checkuser->GetMultiRole);
} else {
    $userrole = [];
}
jlrdw's avatar

Sometimes I like just verifying if a required role is one of the logged-in users roles:

    public static function chkRole($role = null)
    {
        $userrole = Auth::user()->role;
        $checkrole = explode(',', $userrole);
        if (in_array($role, $checkrole)) {
            return true;
        }
        return false;
    }

I know not the answer to your exact question but just thought I would share that with you.

Please or to participate in this conversation.