What are the best practices for handling discounts and form validation in PHP when using DOM objects?

When handling discounts and form validation in PHP using DOM objects, it is important to properly sanitize and validate user input to prevent security vulnerabilities. Additionally, it is crucial to accurately calculate discounts and display them to the user. Using PHP functions such as filter_var() for validation and number_format() for formatting can help ensure the integrity of the data.

<?php
// Validate and sanitize discount input
$discount = filter_var($_POST['discount'], FILTER_SANITIZE_NUMBER_FLOAT);

// Calculate discounted price
$price = 100; // example price
$discountedPrice = $price - ($price * ($discount / 100));

// Format the discounted price
$formattedPrice = number_format($discountedPrice, 2);

// Display the discounted price
echo "Discounted Price: $" . $formattedPrice;
?>