PHP Simple Calculator

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Onaylı Üye
Katılım
24 Haz 2020
Mesajlar
45
Tepki puanı
2
Yaş
34
5 HİZMET YILI
PHP:
function calculator(int $n1, int $n2, string $type): ?int
{
    if (!empty($n1) && !empty($n2) && !empty($type)){
        if ($type == "*"){
            $result = $n1 * $n2;
        }
        if ($type == "/"){
            $result = $n1 / $n2;
        }
        if ($type == "-"){
            $result = $n1 - $n2;
        }
        if ($type == "+"){
            $result = $n1 + $n2;
        }
        return $result;

    }
    return null;
}

echo calculator(2, 4, '+');
 
Uzman Üye
Katılım
20 Mar 2021
Mesajlar
280
Tepki puanı
16
Yaş
39
5 HİZMET YILI
Check this one also have some html.

HTML:
<!DOCTYPE html>

<head>
    <title>Simple Calculator Program in PHP - Tutorials Class</title>
</head>

<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
    switch ($operator) {
        case "Add":
           $result = $first_num + $second_num;
            break;
        case "Subtract":
           $result = $first_num - $second_num;
            break;
        case "Multiply":
            $result = $first_num * $second_num;
            break;
        case "Divide":
            $result = $first_num / $second_num;
    }
}

?>

<body>
    <div id="page-wrap">
    <h1>PHP - Simple Calculator Program</h1>
      <form action="" method="post" id="quiz-form">
            <p>
                <input type="number" name="first_num" id="first_num" required="required" value="<?php echo $first_num; ?>" /> <b>First Number</b>
            </p>
            <p>
                <input type="number" name="second_num" id="second_num" required="required" value="<?php echo $second_num; ?>" /> <b>Second Number</b>
            </p>
            <p>
                <input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
            </p>
            <input type="submit" name="operator" value="Add" />
            <input type="submit" name="operator" value="Subtract" />
            <input type="submit" name="operator" value="Multiply" />
            <input type="submit" name="operator" value="Divide" />
      </form>
    </div>
</body>
</html>
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst