Reputation: 21
I want to make a calculator in PHP with one input field and one submit button.
<html>
<head>
<title>Calculator</title>
</head>
<body>
<form method="post" action="">
<input type="text" name="text1">
<input type="submit" name="btnSubmit">
</form>
</body>
</html>
suppose, if i want add then i will give input 10+12 in the single input and when i will click in submit button it will show result.
<?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";
}
}
?>
i tried a of time but cannot to make the logic.
Upvotes: 2
Views: 2074
Reputation: 1680
You could try to evaluate the string a user gives like this:
<?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';
}
?>
Notes:
This is what the regex in preg_match does:
^[\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) «$»
Upvotes: 2
Reputation: 630
You Can try This Approach Using eval and Loop array:
$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);
Upvotes: 1
Reputation: 674
you should do in the following way because in the if clause your evaluation should be boiled all the way down to either true or false. but your current calculation in the if and elseif clauses aren't doing that.
<?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";
}
}
Upvotes: 0