Can someone please help solve a syntax error using PHP match function to display products?

I am trying to display products by pulling from mysql database. The code uses a function called getProductById that pulls products from the database. The idea is to match the product with the right price and then display the product with the function cartElement. I would like to thank you so much in advance for taking your time to help.

Note: Using PHP Version 7.3.11

Error Message:

Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/cart.php on line 90

My code:

            $total = 0;
            if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
                foreach ($_SESSION['cart'] as $cartProduct) {
                    $product = $database->getProductById((int)$cartProduct['product_id']);
                    $price = match($cartProduct['option']) {
                        'exclusive' => $product['exclusive'],
                        'unlimited' => (int)$product['unlimited'],
                        'premium' => (int)$product['premium'],
                        default => (int)$product['basic'],
                    if (is_numeric($price)) {
                        $total += $price;
                    cartElement($product['product_image'], $product['product_name'],$price,$product['id']);
                    //break; I don't know if I need this...
            } else {
                echo "<h5>Cart is empty.</h5>";

Bind_param() statement giving errors:

//get product by id from database...
public function getProductById(int $id): array{
    //$stmt = $this->con->prepare('SELECT * FROM $tablename WHERE id = ?');
    $sql = "SELECT * FROM Productdb WHERE id = ?";
    $stmt = $this->con->prepare($sql);
    $stmt->bind_param("i", $id);
    $result = $stmt->get_result();
    return $result->fetch_assoc();


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Revise the match statement below

 $price = match($cartProduct['option']) {
                        'exclusive' => $product['exclusive'],
                        'unlimited' => (int)$product['unlimited'],
                        'premium' => (int)$product['premium'],
                        default => (int)$product['basic'],

to read something like this for equivalent functionality as an example

    $price = call_user_func(function($cartProduct, $product) {
         switch($cartProduct['option']??null) {
              case 'exclusive': return $product['exclusive'];
              case 'unlimited' : return (int)$product['unlimited'];
              case 'premium' : return (int)$product['premium'];
              default : return (int)$product['basic'];
    }, $cartProduct, $product);

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x