Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
122 views

Write A Simple Calculator Program in PHP Using Switch Case

The document describes a PHP program that builds a simple calculator. It uses a switch case statement to perform basic math operations - addition, subtraction, multiplication, and division - on two numeric values submitted by a form. The form allows selecting an operation and submitting two numbers, then displays the result.

Uploaded by

Ceh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Write A Simple Calculator Program in PHP Using Switch Case

The document describes a PHP program that builds a simple calculator. It uses a switch case statement to perform basic math operations - addition, subtraction, multiplication, and division - on two numeric values submitted by a form. The form allows selecting an operation and submitting two numbers, then displays the result.

Uploaded by

Ceh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1 PHP

Write a simple calculator program in PHP using switch case

Operations:

 Addition
 Subtraction
 Multiplication
 Division

<!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;
2 PHP

            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>
3 PHP

            <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>

You might also like