【问题标题】:if elseif php simple calculatorif elseif php 简单计算器
【发布时间】:2015-01-28 04:03:07
【问题描述】:

这是计算器的表格

<form method='post' action='result.php' name='calc_form'>

<input type='text' name='input1' size='15'>

<select name='operation'>
<option value="plus">+</option>
<option value="minus">-</option>
<option value="multi">*</option>
<option value="invalid">**</option>
<option value="divide">/</option>
</select>  

<input type='text' name='input2' size='15'>
<input type='submit' value='go'>


</form> 

这是带有 if elseif 语句的 php

<?php

define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');


$result = null;

if 
(isset($_POST) and 
isset($_POST['input1']) and 
isset($_POST['input2']) and 
isset($_POST['operation']))
  {
  $input1 = $_POST['input1'];   
  $input2 = $_POST['input2'];
  $operation = $_POST['operation'];
     switch ($operation)
       {
       case 'plus':
         $result = $input1 + $input2;
         break;
       case 'minus':
         $result = $input1 - $input2;
         break;
       case 'multi':
         $result = $input1 * $input2;
         break;
       case 'divide':
         $result = $input1 / $input2;
         break;
       default: 
         $result = INVALID_OPERATOR;           
         break;
       }
    }  
elseif
(!isset($_POST) and 
isset($_POST['input1']) and 
isset($_POST['input2']) and 
isset($_POST['operation']))
{  
$result = INVALID_INPUT;
}

if ($result !== null)
{
echo <<<EOM


<h2>you calculate</h2> $input1 

<h2>and</h2>  $input2 

<h2>result is:</h2>

$result

EOM;
 }

?>

我什至只尝试了 else 语句并且没有条件,但是当表单中没有添加任何内容时,它不会显示 INVALID INPUT。这里有什么问题?

【问题讨论】:

    标签: php forms if-statement calculator


    【解决方案1】:

    问题出在这段代码上:

    if 
    (isset($_POST) and 
    isset($_POST['input1']) and 
    isset($_POST['input2']) and 
    isset($_POST['operation']))
    

    即使用户在输入字段中没有输入任何内容,他们仍然会传递isset($_POST['input1']),因为它们被“设置”为空字符串。尝试将其切换到此:

    if 
    (isset($_POST['input1']) && strlen($_POST['input1']) &&
     isset($_POST['input2']) && strlen($_POST['input2']) &&
     isset($_POST['operation']))
    

    【讨论】:

    • 是的,这很好用。只是缺少代码的这种和平以避免 php 通知 $input1 = $input2 = $result = null;
    【解决方案2】:

    您可以只使用 else 语句而不是 else if 语句。这样,如果您更改输入,您就不必将每个输入列出两次。

    例如:

    if (isset($_POST) and 
    isset($_POST['input1']) and 
    isset($_POST['input2']) and 
    isset($_POST['operation'])) 
    {
        // result = calculation
    } else
    {
        // result == code
    }
    

    试试这个 - 我在我的服务器上测试过它并且它有效!

    计算器.php

    <?php
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors',1);;
    
    define ('INVALID_INPUT', 'ERROR: invalid input');
    define ('INVALID_OPERATOR', 'ERROR: invalid operator');
    $input1 = $input2 = $operation = null;
    if (isset($_POST['input1']) and  isset($_POST['input2']) and isset($_POST['operation']))
      {
    
        $input1 = $_POST['input1'];   
        $input2 = $_POST['input2'];
        $operation = $_POST['operation'];
        if(is_numeric($input1) and is_numeric($input2))
        {
           switch ($operation)
           {
           case 'plus':
             $result = $input1 + $input2;
             break;
           case 'minus':
             $result = $input1 - $input2;
             break;
           case 'multi':
             $result = $input1 * $input2;
             break;
           case 'divide':
             $result = $input1 / $input2;
             break;
           default: 
             $result = INVALID_OPERATOR;           
             break;
           }
        }
        else 
        {
            $input1 = '';
            $input2 = '';
            $operation = 'invalid';
            $result = INVALID_INPUT;
        }  
    }
    
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body class="">
    
    <form method='POST' action='calculator.php'>
    
    <input type='text' name='input1' size='15' value="<?php echo $input1; ?>">
    
    <select name='operation'>
    <option <?php if($operation == 'plus') { echo 'selected';} ?> value="plus">+</option>
    <option <?php if($operation == 'minus') { echo 'selected';} ?> value="minus">-</option>
    <option <?php if($operation == 'multi') { echo 'selected';} ?> value="multi">*</option>
    <option <?php if($operation == 'invalid') { echo 'selected';} ?> value="invalid">**</option>
    <option <?php if($operation == 'divide') { echo 'selected';} ?> value="divide">/</option>
    </select>  
    
    <input type='text' name='input2' size='15' value="<?php echo $input2; ?>">
    <span>=</span>
    <input type='text' name='result' size='50' value="<?php echo $result; ?>">
    
    <input type='submit' value='go'>
    
    
    </body>
    </html>
    

    【讨论】:

    • 结果是一样的,显示零而不是INVALID INPUT
    • 这是不同的标记,但它工作正常。只需添加 $input1 = $input2 = $operation = $result = null;该代码行中的结果变量。
    • 我忘记把get方法改成post方法了。
    【解决方案3】:

    你的条件不对;这是不可能的:

    !isset($_POST) and 
    isset($_POST['input1']) and 
    isset($_POST['input2']) and 
    isset($_POST['operation'])
    

    我怀疑你的意思

    !isset($_POST) or 
    !isset($_POST['input1']) or 
    !isset($_POST['input2']) or 
    !isset($_POST['operation'])
    

    【讨论】:

    • 我试过了,但是当表单为空而不是 INVALID INPUT 时,结果显示为零
    猜你喜欢
    • 2014-02-22
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多