Hướng dẫn calculator by switch case in php - máy tính theo trường hợp chuyển đổi trong php

Sự mô tả:

Bạn cần viết một chương trình máy tính đơn giản trong PHP bằng cách sử dụng trường hợp chuyển đổi.

Operations:

  • Phép cộng
  • Phép trừ
  • Phép nhân
  • Phân công

Xem giải pháp/chương trình

<!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">
	<h2>PHP - Simple Calculator Program</h2>
	  <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>

Lớp hướng dẫn - Cửa sổ đầu ra

Hướng dẫn calculator by switch case in php - máy tính theo trường hợp chuyển đổi trong php


Tìm hiểu thêm về các chủ đề tương tự:

Hướng dẫn
Ra quyết định PHP
Bài tập & bài tập
Viết một chương trình để kiểm tra lớp học sinh dựa trên điểm
Viết một chương trình để hiển thị ngày trong tuần bằng cách sử dụng Switch
Viết một chương trình tính toán hóa đơn tiền điện trong PHP
Viết một chương trình máy tính đơn giản trong PHP bằng cách sử dụng trường hợp chuyển đổi
Viết chương trình PHP để kiểm tra xem một người có đủ điều kiện để bỏ phiếu không
Viết chương trình PHP để kiểm tra xem một số có dương, âm hay không
Phỏng vấn câu hỏi & câu trả lời
Không tìm thấy nội dung.

Tôi đã tạo một máy tính bằng cách sử dụng câu lệnh if trong PHP và tôi đã có thể nhận được đầu ra tôi cần. Tôi bị mắc kẹt với trường hợp chuyển đổi. Tôi không nhận được đầu ra mà tôi muốn.

Đây là mã của tôi:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>

Hướng dẫn calculator by switch case in php - máy tính theo trường hợp chuyển đổi trong php

B. Desai

16.3k5 Huy hiệu vàng25 Huy hiệu bạc47 Huy hiệu đồng5 gold badges25 silver badges47 bronze badges

Bạn đang vượt qua "+" và "-" vào công tắc và trường hợp là "bổ sung" và "phụ". Họ không phù hợp.Mar 21, 2018 at 5:44

Hướng dẫn calculator by switch case in php - máy tính theo trường hợp chuyển đổi trong php

2

Đã trả lời ngày 21 tháng 3 năm 2018 lúc 6:06
2) Whenever you submit your form it takes the value + & - in

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
1 value.
3) In switch case you mentioned cases :
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
2 &
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
3, where your post value having + & -. Which not satisfying any cases.
4) Just replace your + & - with
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
2 &
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
3 respectively in your form input value like this

<input type="submit" name="submit" value="addition">
<input type="submit" name="submit" value="sub">

WilswilsMar 21, 2018 at 6:03

1.1687 Huy hiệu bạc24 Huy hiệu đồngAshish Tiwari

Tôi đã tạo một máy tính bằng cách sử dụng câu lệnh if trong PHP và tôi đã có thể nhận được đầu ra tôi cần. Tôi bị mắc kẹt với trường hợp chuyển đổi. Tôi không nhận được đầu ra mà tôi muốn.1 gold badge13 silver badges24 bronze badges

<!DOCTYPE HTML>
<html>
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Untitled Document</title>
   <?php 

      $num1 = 0;
      $num2 = 0;
      $calc = 0;

      if (isset($_POST['submit']))
      {
        $num1 = $_POST['n1'];
        $num2 = $_POST['n2'];
        $calc =  calculate($num1, $num2, $_POST['submit']);
      }

     function calculate($num1,$num2,$op) 
     {
       $calc = 0;

       switch ($op) 
       {
          case '+':   
                    $calc = $num1 + $num2;
                    break;
          case '-':
                    $calc = $num1 - $num2;
          break;
       }

       return $calc;

    }
  ?>
 </head>

 <body>

    <form action="" method="post">
       NO 1 : <input  name='n1' value="<?php echo $num1;?>">
       <br><br>
       NO 2: <input  name='n2' value="<?php echo $num2;?>"><br><br>
       total: <input name="res" value="<?php echo $calc;?>"><br><br>
       <input type="submit" name="submit" value="+">
       <input type="submit" name="submit" value="-">

   </form>
 </body>
</html>

Đây là mã của tôi:Mar 21, 2018 at 6:09

B. DesaiBilal Sipra

Đã hỏi ngày 21 tháng 3 năm 2018 lúc 5:442 silver badges12 bronze badges

1) Bạn đã đưa ra giá trị gửi + & -. 2) Bất cứ khi nào bạn gửi biểu mẫu của mình, bạn sẽ lấy giá trị + & - trong giá trị

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
1. 3) Trong trường hợp chuyển đổi, bạn đã đề cập các trường hợp:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
2 &
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
3, trong đó giá trị bài của bạn có + & -. Mà không thỏa mãn bất kỳ trường hợp. 4) Chỉ cần thay thế + & - của bạn bằng
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
2 &
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2,$calc){
    switch ($_POST['submit']) {
        case 'addition':
            $calc = $n1 + $n2;
            break;

        case 'sub':
            $calc = $n1 - $n2;
            break;
    }

}
}
?>
</head>

<body>

    <form action="" method="post">
        NO 1 : <input  name='n1' value="<?php echo $num1;?>">
        <br><br>
        NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
        total: <input type="res" value="<?php echo $calc;?>"><br><br>
        <input type="submit" name="submit" value="+">
        <input type="submit" name="submit" value="-">

    </form>
</body>
</html>
3 tương ứng trong giá trị đầu vào biểu mẫu của bạn như thế này

Đã trả lời ngày 21 tháng 3 năm 2018 lúc 6:03

2) Vấn đề tên biến.

3) Gửi giá trị nút và trường hợp chuyển đổi không bao giờ khớp, v.v.

Bạn cần thay đổi mã của mình như dưới đây:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<?php 
$num1 = "";
$num2 = "";
$calc = "";

if (isset($_POST['submit']))
{

 $num1 = $_POST['n1'];
 $num2 = $_POST['n2'];

function calculate($num1,$num2){
  $calc = "";
  switch ($_POST['submit']) {

    case 'addition':
      $calc = $num1 + $num2;
      break;

    case 'sub':
      $calc = $num1 - $num2;
      break;
  }
return $calc;
}
$calc = calculate($num1,$num2);
}
?>
</head>

<body>

  <form action="" method="post">
    NO 1 : <input  name='n1' value="<?php echo $num1;?>">
    <br><br>
    NO 2: <input  name='n2' value="<?php echo $num2?>"><br><br>
    total: <input type="res" value="<?php echo $calc;?>"><br><br>
    <input type="submit" name="submit" value="addition">
    <input type="submit" name="submit" value="sub">

    </form>
</body>
</html>

Đã trả lời ngày 21 tháng 3 năm 2018 lúc 5:56Mar 21, 2018 at 5:56

Hướng dẫn calculator by switch case in php - máy tính theo trường hợp chuyển đổi trong php

B. Desaib. DesaiB. Desai

16.3k5 Huy hiệu vàng25 Huy hiệu bạc47 Huy hiệu đồng5 gold badges25 silver badges47 bronze badges

0

Bạn đang vượt qua "+" và "-" vào công tắc và trường hợp là "bổ sung" và "phụ". Họ không phù hợp.

Đã trả lời ngày 21 tháng 3 năm 2018 lúc 6:06Mar 21, 2018 at 6:06

Hướng dẫn calculator by switch case in php - máy tính theo trường hợp chuyển đổi trong php

WilswilsWils

1.1687 Huy hiệu bạc24 Huy hiệu đồng7 silver badges24 bronze badges