본문 바로가기
PHP

function

by 영감은어디에 2024. 10. 9.

<?php
$candy = [
    'Toffee' => ['price' => 3, 'stock' => 12],
    'Mints'  => ['price' => 2, 'stock' => 26],
    'Fudge'  => ['price' => 4, 'stock' => 8],
];
$tax = 20;

function get_reorder_message(int $stock): string
{
    return ($stock < 10) ? 'Yes' : 'No';
}

function get_total_value(float $price, int $quantity): float
{
    return $price * $quantity;
}

function get_tax_due(float $price, int $quantity, int $tax = 0): float
{
    return ($price * $quantity) * ($tax / 100);
}
?>

<table>
      <tr>
        <th>Product</th><th>Stock</th><th>Re-order</th><th>Total value</th><th>Tax due</th>
      </tr>
      <?php foreach ($candy as $product_name => $data) { ?>
        <tr>
          <td><?=  $product_name ?></td>
          <td><?=  $data['stock'] ?></td>
          <td><?=  get_reorder_message($data['stock']) ?></td>
          <td>$<?= get_total_value($data['price'], $data['stock']) ?></td>
          <td>$<?= get_tax_due($data['price'], $data['stock'], $tax) ?></td>
       </tr>
      <?php } ?>
</table>

'PHP' 카테고리의 다른 글

sql  (0) 2024.10.11
date  (0) 2024.10.10
image file  (0) 2024.10.10
form  (0) 2024.10.10
내장함수, number, update  (0) 2024.10.10
object  (0) 2024.10.09
foreach  (0) 2024.10.09
PC에 XAMPP용 Imagick, ImageMagick 설치 하는법  (0) 2024.06.27