This uses FILTER_VALIDATE_INT (not intval() ), which distinguishes between 0 , null , and false . It rejects decimals, strings, and empty values explicitly. 2.2. Checking Inventory Before Adding A premium addcartphp script never assumes stock. It queries the database live.
// Re-check stock against new total if ($new_quantity > $product['stock_quantity']) die(json_encode(['error' => 'Cannot add. Total would exceed stock.'])); addcartphp num high quality
// If product already in cart, update quantity (add to existing) if (isset($_SESSION['cart'][$product_id])) $new_quantity = $_SESSION['cart'][$product_id]['quantity'] + $num; This uses FILTER_VALIDATE_INT (not intval() )
$_POST['num'] = 'abc'; include 'add_to_cart.php'; $this->expectOutputRegex('/Invalid quantity/'); which distinguishes between 0