<?php /*
   +------------------------------------------------------------------+
   | Green-Beast.com                                                  |
   | PHP: Simple Calculator                                           |
   | PHP Hypertext Preprocessor                                       |
   | Copyright Dec 2007                                               |
   | Use with attribution by visible link please!                     |
   | Attribute to: <a href="http://green-beast.com/">Mike Cherim</a>  |
   +------------------------------------------------------------------+
*/
?>

<?php
/*
* INSTRUCTIONS:
*
* There's nothing to configure. Put this on your server and include it
* directly above the form. Alternatively you could combine the PHP
* followed by the XHTML is a single file, then include IT
*/
?>

<?php
// Default output value

    
$theoutput = "";

// If the form is posted, grab those values and start script operations

if(@$_POST['psc']) {

    
$input_one = $_POST['i1'];
    
$input_two = $_POST['i2'];
    
$operation = $_POST['op'];

// Trim these in case a space is added, strip_tags just in case

    
$input_one = trim(strip_tags($input_one));
    
$input_two = trim(strip_tags($input_two));

// Make a reset link

    
$the_reset = ' <a href="'.htmlentities($_SERVER["REQUEST_URI"]).'">Reset</a>';

// Do the basic math operations (probably a way to combine into one string but I couldn't figure it out

    
if ($operation == "+") {
        
$theanswer = $input_one + $input_two;
    }

    if (
$operation == "-") {
        
$theanswer = $input_one - $input_two;
        
$operation = "&#8211;"; // To display a better symbol
    
}

    if (
$operation == "*") {
        
$theanswer = $input_one * $input_two;
        
$operation = "&#215;"; // To display a better symbol
    
}

    if (
$operation == "/") {
        
$theanswer = $input_one / $input_two;
        
$operation = "&#247;"; // To display a better symbol
    
}

// In case a bot submits large values - people are limited by the input maxlengths

    
if (strlen($input_one) > 10 || strlen($input_two) > 10 ) {
        
$theoutput = ' <p class="error"><strong>Error:</strong> Input limit exceeded.'.$the_reset.'</p>';  
    } else

// In case the inputs are left empty... can't add air, right?

    
if( empty($input_one) || empty($input_two) ) {
        
$theoutput = ' <p class="error"><strong>Error:</strong> You must enter values.'.$the_reset.'</p>';
    } else

// In case someone tries letter math or other stuff
// There is a better way to do this. If you know how, please email me at "mikecherim" at my green-beast.com domain

    
if( !preg_match("([0-9])", $input_one) || !preg_match("([0-9])", $input_two) || preg_match("([a-z])i", $input_one ) || preg_match("([a-z])i", $input_two ) ) {
       
$theoutput = ' <p class="error"><strong>Error:</strong> Enter numbers only.'.$the_reset.'</p>';
    }

// All's well, let's create an output string
    
else {
        
$theoutput = " <p><strong>Output: </strong>"."$input_one"." $operation "."$input_two"." = "."$theanswer $the_reset"."</p>";
    }

}
?>