phpfunctionrewriting

How can we change this PHP function to work with PHP 8?


Would anyone know how we can rewrite the following function to be compatible and work with PHP 8?

function wpsc_have_shipping_quote() {

    $has_quote = false;

    global $wpsc_cart;

    if ( $wpsc_cart->shipping_quote_count > 0 || count( $wpsc_cart->shipping_quotes ) > 0 ) {
        $has_quote = true;
    }

    return $has_quote;
}

Solution

  • You don't have to count the array just to see if it has anything in it. When you use ||, the values being compared will be converted to booleans, and shipping_quotes will evaluate to false whether it is an empty array or a null.

    function wpsc_have_shipping_quote() {    
    
        global $wpsc_cart;
    
        return $wpsc_cart->shipping_quote_count || $wpsc_cart->shipping_quotes;
    }
    

    This should work in any PHP version.