In what ways can PHP be leveraged to optimize the calculation and display of ingredient quantities based on user input in a recipe management system like the one described in the thread?

To optimize the calculation and display of ingredient quantities based on user input in a recipe management system, PHP can be leveraged to dynamically adjust the quantities based on the serving size selected by the user. This can be achieved by storing the base quantities of ingredients in the database and then using PHP to calculate the adjusted quantities based on the ratio of the selected serving size to the default serving size.

// Assuming $defaultServings and $ingredientQuantity are already defined
$userServings = $_POST['servings']; // Assuming the user selects the serving size via a form input

// Calculate the ratio of user servings to default servings
$ratio = $userServings / $defaultServings;

// Adjust ingredient quantities based on the ratio
$adjustedQuantity = $ingredientQuantity * $ratio;

// Display the adjusted quantity
echo "Adjusted Quantity: " . $adjustedQuantity;