In this section, we are going to create a simple function that will calculate the discount of a given percentage. To do this, follow these steps:
function.php
.<?php ?>
$sweaterPrice
and $precentOff. T
hey will store the original price of the product, as well as the percent off:<?php $sweaterPrice = 50; $percentOff = 0.25; ?>
<?php $sweaterPrice = 50; $percentOff = 0.25; function couponCode($price, $discount){ return $price * $discount; } ?>
<?php $sweaterPrice = 50; $percentOff = 0.25; function couponCode($price, $discount){ return $price * $discount; } echo "The sweater originally costs $" . $sweaterPrice . " with the discount you'll pay $" . ($sweaterPrice - couponCode($sweaterPrice, $percentOff)) . " "; ?>
Now that you have an understanding of functions, you should be comfortable with developing reusable code and applying them. In the next section, we will learn about classes. Classes will give us a better understanding of structuring code and properties into a neat package.