2016-12-17 65 views
2

我想用一個輸入字段和一個提交按鈕在PHP中創建一個計算器。在php中輸入一個輸入的計算器

<html> 
<head> 
    <title>Calculator</title> 
</head> 
<body> 
    <form method="post" action=""> 
     <input type="text" name="text1"> 
     <input type="submit" name="btnSubmit"> 
    </form> 
</body> 
</html> 

想,如果我想加入的話,我會給輸入10 + 12在單輸入,當我將點擊提交按鈕會顯示結果。

<?php 
if(isset($_POST["btnSubmit"])){ 
    $a=$_POST["text1"]; 
    if($a+$a){ 
     echo $a+$a; 
    }elseif($a-$a){ 
     echo $a-$a; 
    }elseif($a*$a){ 
     echo $a*$a; 
    }elseif($a/$a){ 
     echo $a/$a; 
    }else{ 
     echo "false"; 
    } 
} 

?> 

我試過了一段時間,但不能做出邏輯。

+0

所以,當人將輸入2.然後,在什麼基礎上它會加/減。而且,至少需要2個數字來執行計算?它背後的想法是什麼? –

+2

從你的想法開始'if($ a + $ a){'should do – Federkun

+1

代碼是各種各樣的錯誤。如果特別煩我。在PHP http://php.net/manual/ro/language.types.boolean.php這些值不會做你期望他們做的。你有更多的閱讀要做。 – TigOldBitties

回答

0

您應該按照以下方法進行操作,因爲在if子句中,您的評估應該一直歸結爲真或假。但是您在if和elseif子句中的當前計算沒有這樣做。

<?php 

if(isset($_POST["btnSubmit"])){ 
    $a = $_POST["text1"]; 



    // first get the what operator you have 
    // extract the first number or the numbers left side of the operator 
    // extract the last number or the numbers right side of the operator 
    $operator = false; 
    if (stripos($a, "+")) { 
     $operator = substr($a, stripos($a, "+"), 1); 
     $firstNumber = substr($a, 0, stripos($a, "+")); 
     $lastNumber = substr($a, stripos($a, "+")+1); 
    } elseif (stripos($a, "-")) { 
     $operator = substr($a, stripos($a, "-"), 1); 
     $firstNumber = substr($a, 0, stripos($a, "-")); 
     $lastNumber = substr($a, stripos($a, "-")+1); 
    } elseif (stripos($a, "*")) { 
     $operator = substr($a, stripos($a, "*"), 1); 
     $firstNumber = substr($a, 0, stripos($a, "*")); 
     $lastNumber = substr($a, stripos($a, "*")+1); 
    } elseif (stripos($a, "/")) { 
     $operator = substr($a, stripos($a, "/"), 1); 
     $firstNumber = substr($a, 0, stripos($a, "/")); 
     $lastNumber = substr($a, stripos($a, "/")+1); 
    } 

    // make sure that user submited operator is one of the above operators 
    if ($operator == false) 
     die("Wrong operator"); 




    if($operator == '+') { 
     echo ($firstNumber + $lastNumber); 
    } else if ($operator == '-') { 
     echo ($firstNumber - $lastNumber); 
    } else if ($operator == '*') { 
     echo ($firstNumber * $lastNumber); 
    } else if ($operator == '/') { 
     echo ($firstNumber/$lastNumber); 
    } else { 
     echo "false"; 
    } 
} 

substr function documentation

stripos documentation

+0

thx很多,但它唯一的工作用於添加。它不適用於減法,乘法或除法。 –

+0

現在它可以做四個操作。 – xFighter

+0

我已經完成了從上次的幫助中獲得幫助,但非常感謝您的全力支持。再次感謝 –

2

你可以嘗試評估字符串用戶給出這樣的:

<?php 

// Initialise $POST['text1'] for testing purpose 
$POST['text1'] = '12*-10'; 

// IMPORTANT --------------------------------------------------------------- 
// It could be dangerous to evaluate text input by a user. 
// So, we make sure to evaluate input of the form 
// "numeric_value operator numeric_value" only. 
// IMPORTANT --------------------------------------------------------------- 
if (preg_match('%^[\d-+.]*?[-+*/]{1,1}[\d-+.]*$%', $POST['text1'])) { 
    // Prepare a PHP statement containing the user's term. 
    // I.e. "return a + b;" 
    $term = 'return ' . $POST['text1'] . ';'; 
    // Evaluate the statement now. 
    $res = eval($term); 
    if ($res!==false) { 
     // Evaluation done correctly. 
     echo $res; 
    } else { 
     // Error in PHP statement. 
     echo 'Illegal term. Format "numeric_value operator numeric_value"'; 
    } 
} else { 
    echo 'Illegal characters in term'; 
} 

?> 

注:

  • eval優雅方法在這種情況下。
  • 這也是一個危險解決方案,如果你不確保只有適當的表達式進行評估。否則,用戶的輸入可能是有害的,因爲他/她可能會執行任何可能的PHP語句。因此,代碼是爲了確保只有預期的輸入進行處理:
    如果(的preg_match(...))

這是正則表達式的preg_match做什麼:

^[\d-+.]*?[-+*/]{1,1}[\d-+.]*$ 

Assert position at the beginning of the string «^» 
Match a single character present in the list below «[\d-+.]*?» 
    Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
    A single digit 0..9 «\d» 
    One of the characters 「-+.」 «-+.» 
Match a single character present in the list below «[\d-+*/]{1,1}» 
    Exactly 1 times «{1,1}» 
    One of the characters 「-+*/」 «-+*/» 
Match a single character present in the list below «[\d-+.]*» 
    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
    A single digit 0..9 «\d» 
    One of the characters 「-+.」 «-+.» 
Assert position at the end of the string (or before the line break at the end of the string, if any) «$» 
+0

我正要用eval()寫答案。但是,你的回答比我想要的要好。 *豎起大拇指* –

+0

@NanaPartykar:謝謝你的花朵。 – hherger

1

你可以嘗試這種方法使用eval和Loop數組:

$string = '61+6-55*1+2-14+1'; 
    $num = preg_split("/[^0-9]+/", $string); 
    $op = (array_filter(preg_split("/[0-9]+/", $string))); 
    $a = $num[0]; 
    $res = 0; 
    foreach ($op as $key => $val) { 
     $b = $num[$key]; 
     $res = eval("return $a $val $b;"); 
     $a = $res; 
    } 
    var_dump($res);