I want to create a calculator script that allows the user to submit two numbers an choose an operation to perform on them(addition, multiplication,division,subtraction).
I used a hidden field display the number of requests that the user has submittted(Is my English clear enough?) ,Here is my code:

<?php
$operator="";
$num1;
$num2;
$num_tries=(isset($num_tries))?++$num_tries:0;
//$num_tries is for record the number of times user aubmit this form

function calculator($num1,$num2,$operator)
{
if ($num1==int&&$num2==int&&!empty($operator))
{ $result= $num1$operator$num2;} // How can I express the equation here?

else{
$result="Please select operator and input number in two box");
}
return $result;
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post">
<input type="text" name="num1" value="<?php print $num1?>">
<select name="operator">
<option value="+">
<option value="-">
<option value="*">
<option value="/">
<input type="text" name="num2" value="<?php print $num2?>">
<input type="submit" value="=">
<?php print calculator();?>
<input type="hidden" name="num_tries" value="<?php print $num_tries?>">

</body>
</html>

Thank you for reading my code, you can just tell me what reference I should look. Thank you !😕 😕

    Hi,

    use the eval function:

    replace

    { $result= $num1$operator$num2;}

    by { eval("\$result = \$num1 ".$operator." \$num2;"); }

      Thanks a lot! the eval works fine.The operator in HTML form still can't display here is my code now:
      <?php
      $operator="";
      $num1;
      $num2;
      $num_tries=(isset($num_tries))?++$num_tries:0;
      //$num_tries is for record the number of times user aubmit this form
      function calculator($num1,$num2,$operator)
      {
      if ($num1==int&&$num2==int&&!empty($operator))
      { eval("\$result=\$num1".$operator."\$num2;");} // How can I express the equation here?

      else{
      $result="Please select operator and input number in two box";
      }
      return $result;
      }
      ?>
      <html>
      <head>
      <title>
      </title>
      </head>
      <body>
      <form method="post">
      <input type="text" name="num1" value="<?php print $num1?>">
      <select name="operator">
      <option value="+">
      <option value="-">
      <option value="*">
      <option value="/">
      <input type="text" name="num2" value="<?php print $num2?>">
      <input type="submit" value="=">
      <?php print calculator($num1,$num2,$operator);?>
      <input type="hidden" name="num_tries" value="<?php print $num_tries?>">

      </body>
      </html>

      red

        Hi,

        I think there is a </select> missing after the last option
        and it should look like that:

        <select name="operator">
        <option value="+">+</option>
        <option value="-">-</option>
        <option value=""></option>
        <option value="/">/</option>
        </select>

          Write a Reply...