Is it recommended to encapsulate the delete functionality in a separate PHP function for better code organization?

It is recommended to encapsulate the delete functionality in a separate PHP function for better code organization. This helps in keeping the code modular, reusable, and easier to maintain. By isolating the delete logic in a separate function, it can be easily called whenever needed without duplicating code.

function deleteItem($item_id) {
    // Connect to database
    $conn = new mysqli('localhost', 'username', 'password', 'database');

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Prepare and execute delete query
    $sql = "DELETE FROM items WHERE id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $item_id);
    $stmt->execute();

    // Close connection
    $stmt->close();
    $conn->close();
}