Thursday, March 8, 2012

How to make a simple calculator function using php?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Simple Calculator function</title>
  </head>
  <body>
<?php
function showForm() {
?>
    <table border="0" cellpadding="2" cellspacing="2" style="font:12px Arial, Helvetica, sans-serif">
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
        <tr>
            <td width="111" align="right" bgcolor="#CCCCCC"><strong>Number:</strong></td>
            <td width="293" bgcolor="#CCCCCC"><input type="text" maxlength="3" name="number" size="4" value="<?=$_POST['number'];?>"  /></td>
        </tr>
       
        <tr>
            <td align="right" bgcolor="#CCCCCC"><strong>Another number:</strong></td>
            <td bgcolor="#CCCCCC"><input type="text" maxlength="4" name="number2" size="4"  value="<?=$_POST['number2'];?>"/></td>
        </tr>
       
        <tr>
            <td align="right" valign="top" bgcolor="#CCCCCC"><strong>Operator:</strong></td>
          <td bgcolor="#CCCCCC"><table width="100%" border="0" cellspacing="2" cellpadding="2">
            <tr>
              <td><input type="radio" name="opt" value="+" />+</td>
              <td><input type="radio" name="opt" value="-"   />-</td>
              <td><input type="radio" name="opt" value="*"  />*</td>
              <td><input type="radio" name="opt" value="/" />/</td>
            </tr>
            <tr>
              <td><input type="radio" name="opt" value="^2" />x<sup>2</sup></td>
              <td><input type="radio" name="opt" value="sqrt" />sqrt</td>
              <td><input type="radio" name="opt" value="^" />^</td>
              <td>&nbsp;</td>
            </tr>
          </table> </td>
        </tr>
        <tr>
            <td align="right" bgcolor="#CCCCCC"><strong>Rounding:</strong></td>
            <td colspan="2" bgcolor="#CCCCCC"><input type="text" name="rounding" value="4" size="4" maxlength="4" /> <small>(Enter how many digits you would like to round to)</small>            </td>
          </tr>
       
        <tr>
            <td align="right" bgcolor="#CCCCCC">&nbsp;</td>
            <td bgcolor="#CCCCCC"><input type="submit" value="Calculate" name="submit" /></td>
        </tr>
        </form>
    </table>
    <?php
}

if (empty($_POST['submit'])) {
    showForm();
} else {
showForm();
    $errors = array();
    $error = false;
   
    if (!is_numeric($_POST['number'])) {
        (int)$_POST['number'] = rand(1,200);
    }
   
    if (empty($_POST['number'])) {
        (int)$_POST['number'] = rand(1,200);
    }
   
    if (!is_numeric($_POST['number2'])) {
        (int)$_POST['number2'] = rand(1,200);
    }
   
    if (empty($_POST['number2'])) {
        (int)$_POST['number2'] = rand(1,200);
    }
   
    if (empty($_POST['rounding'])) {
        $round = 0;
    }
   
    if (!isset($_POST['opt'])) {
        $errors[] = "You must select an operation.";
        $error = true;
    }
   
    if (strpbrk($_POST['number'],"-") and strpbrk($_POST['number2'],"0.") and $_POST['opt'] == "^") {
        $errors[] = "You cannot raise a negative number to a decimal, this is impossible. <a href=\"http://www.tuxradar.com/practicalphp/4/6/4\">Why?</a>";
        $error = true;
    }
   
    if ($error != false) {
        echo "We found these errors:";
        echo "<ul>";
        foreach ($errors as $e) {
            echo "<li>$e</li>";
        }
        echo "</ul>";
    } else {
        switch ($_POST['opt']) {
            case "+":
                $result = (int)strip_tags($_POST['number']) + (int)strip_tags($_POST['number2']);
                echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)strip_tags($_POST['number2']) . " is $result.";
                break;
            case "-";
                $result = (int)strip_tags($_POST['number']) - (int)strip_tags($_POST['number2']);
                echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)strip_tags($_POST['number2']) . " is $result.";
                break;
            case "*";
                $result = (int)strip_tags($_POST['number']) * (int)strip_tags($_POST['number2']);
                echo "The answer to " . (int)strip_tags($_POST['number']) . " $_POST[opt] " . (int)$_POST['number2'] . " is $result.";
                break;
            case "/";
                $result = (int)strip_tags($_POST['number']) / (int)strip_tags($_POST['number2']);
                $a = ceil($result);
                    echo "<br />";
                    echo "<hr />";
                    echo "<h2>Rounding</h2>";
                    echo "$result rounded up is $a";
                    echo "<br />";
                    $b = floor($result);
                    echo "$result rounded down is $b";
                    echo "<br />";
                    $h = round($result,(int)$_POST['rounding']);
                    echo "$result rounded to $_POST[rounding] digits is " . $h;
                break;
            case "^2":
                $result = (int)strip_tags($_POST['number']) * (int)strip_tags($_POST['number2']);
                $a = (int)$_POST['number2'] * (int)$_POST['number2'];
                    echo "The answer to " . (int)$_POST['number'] . "<sup>2</sup> is " . $result;
                    echo "<br />";
                    echo "The answer to " . (int)$_POST['number2'] . "<sup>2</sup> is " . $a;
                break;
            case "sqrt":
                $result = (int)strip_tags(sqrt($_POST['number']));
                $sqrt2 = (int)strip_tags(sqrt($_POST['number2']));
                echo "The square root of " . (int)strip_tags($_POST['number']) . " is " . $result;
                    echo "<br />";
                    echo "The square root of " . (int)strip_tags($_POST['number2']) . " is " . $sqrt2;
                    echo "<br />";
                    echo "The square root of " . (int)strip_tags($_POST['number']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($result,(int)$_POST['rounding']);
                    echo "<br />";
                    echo "The square root of " . (int)strip_tags($_POST['number2']) . " rounded to " . strip_tags($_POST[rounding]) . " digits is " . round($sqrt2,(int)strip_tags($_POST['rounding']));
                break;
            case "^":
                $result = (int)strip_tags(pow($_POST['number'],$_POST['number2']));
                $pow2 = (int)strip_tags(pow($_POST['number2'],$_POST['number']));
                echo (int)$_POST['number'] . "<sup>" . strip_tags($_POST[number2]) . "</sup> is " . $result;
                    echo "<br />";
                    echo (int)$_POST['number2'] . "<sup>" . strip_tags($_POST[number]) . "</sup> is " . $pow2;
                    break;
        }
           
    }
}
?>
  </body>
</html>

No comments:

Post a Comment