So, I've been trying to learn PHP. I've got an assignment where I'm supposed to make a list of products with prices, and depending on what day it is, it's supposed to give you a discount. That works so far. However, one thing i'm trying to do is to not echo the result directly in the code; I want to create a function that takes the necessary parameters (Taste, Price, Amount) and returns a formatted string. I haven't been able to do this, despite trying a lot. Here's the code I have. This is the first time i'm using Stack Overflow, so forgive me if i didn't type it in correctly.
<?php
$donuts = array(
array("Strawberry", 10),
array("Chocolate", 20),
array("Vanilla", 30)
);
$weekday = date('N');
$week = date('W');
$hour = date('G');
if ($weekday == 4) {
echo "Hi";
}
?>
<?php
$today = date("l F j, Y");
if ($weekday % 2 == 1 && $week % 2 == 1 && $hour >= 14 && $hour < 17){
echo "Congratulations! You get a 5% discount, and your wares will be delivered tomorrow!";
}
PRINT "$today";
?>
<table border="1">
<tr>
<th>Taste</th>
<th>Price</th>
<th>Discount</th>
<th>Amount</th>
</tr>
<?php
for($i = 0; $i<count($donuts); $i++) {
echo "<tr>";
echo "<td>".$donuts[$i][0]."</td>";
if( $weekday == 1){
echo"<td>".getPricePercent($donuts[$i][1], .5)." kr</td>";
}
if( $weekday == 3){
echo"<td>".getPricePercent($donuts[$i][1], 1.1)." kr</td>";
}
$pris = $donuts[$i][1];
if ($weekday == 5 && $pris > 20){
echo"<td>".($pris-1)." kr</td>";
}
else { echo"<td>".getPrice($donuts[$i][1], .9)." kr</td>";}
?>
<td><input type="number" name="donuts-<?php echo $i; ?>" /></td>
<?php
echo "</tr>";
}
?>
</table>
<?php
function getPrice($price, $percent) {
return ($price);
}
function getPricePercent($price, $percent){
return ($price * $percent);
}
?>