Developing API methods to return JSON When developing API methods to return JSON with details I often find myself creating the following code:

    $response = array('success' =&gt; false);

    $user = User::getSessionUser();
    if($user-&gt;id &gt; 0){
        if(ItemSKU::stringIsValid($sku)){

            $itemSKU = ItemSKU::getFromString($sku);
            if($itemSKU-&gt;isAvailable()){
                if(Carts::add($user-&gt;id, $sku)){
                    $response['success'] = true;
                }
                else{
                    $response['error'] = "Item was already added";
                }
            }
            else{
                $response['error'] = "Unavailable Item";
            }
        }
        else{
            $response['error'] = "Invalid SKU";
        }
    }
    else{
        $response['error'] = "Invalid User";
    }

    return response()-&gt;json($response);


How can I avoid using this nested if-else chain?
Personally I prefer avoiding such nested if-else constructs, simply by reversing the logic, e.g.:

if(!cond1)
    return value;
if(!cond2)
    return value2;
// etc.


instead of:

 if (cond1)
     if(!cond2)
         return value2;
 else
     return value;