PHP

This code will perforform basic arithmetic functions after asking for 2 numbers

 

Division by zero is expected if the user will put 0 (zero) as second number (num2)

<?php
set_error_handler("warning", E_WARNING);
function warning($errno, $errstr) { }

Let's create a class that will handle our form values. The numParam->input-value is used to get the values from the form regardless of its method (get or post)

class numParam {
	public $action;
    public $num1;
	public $num2;
    function input_value($field, $errorMessage = null, $trimInput = true) {
        return
        (
                ( isset ($_GET[$field]) ) ?
                    (   ($trimInput)  ?
                        ((trim($_GET[$field] ) == "") ?
                            $errorMessage
                        :
                            $_GET[$field]
                        )
                    : (($_GET[$field]  == "") ?
                        $errorMessage
                :
                    $_GET[$field]
                    ))
            :
                ((isset($_POST[$field])) ?
                    (    ($trimInput) ?
                        ((trim($_POST[$field]) == "") ? $errorMessage : $_POST[$field])
                    :
                        (($_POST[$field] == "") ? $errorMessage : $_POST[$field]))
                :
                    $errorMessage)
        );
    }
}

A numberResult class will be used to handle our results. numberResult->numProcess is used to perform arithmetic operations

class numbersResult extends numParam  {
	public $add;
	public $subtract;
	public $divide;
	public $multiply;
    function numProcess(numParam $form)
    {
        $this->num1 = $form->num1;
        $this->num2 = $form->num2;
        $this->add = $this->num1 + $this->num2;
        $this->subtract = $this->num1 - $this->num2;
        $this->divide = $this->num1 / $this->num2;
        $this->multiply = $this->num1 * $this->num2;
        return $this;
    }
}

Make an instance of numParam, and populate it.

$form = new numParam();
$form->action = $form->input_value("action");
$form->num1 = $form->input_value("num1");
$form->num2 = $form->input_value("num2");

Wait for the user to invoke the compute function before doing the arithmetic operation

if ($form->action == "compute") {
	$result = new numbersResult;
    $total = $result->numProcess($form);
}
?>

Display the User Interface

<html>
<head>
    <title>PHP Objects</title>
</head>
<body>
    <form method="post">
        <b>Num1</b>
        <input type="text" name="num1" value="<?=$form->num1?>" />
        <br />
        <b>Num2</b>
        <input type="text" name="num2" value="<?=$form->num2?>" />
        <p />
        <input type="submit" name="action" value="compute" />
    </form>

    <?php
    if ($form->action  == "compute") {
    ?>
    <hr />
    <table cellpadding="5" cellspacing="5" border="0">
        <tr>
            <td>
                <b>Addition</b>
            </td>
            <td>
                <?=$total->num1?>
            </td>
            <td>+</td>
            <td>
                <?=$total->num2?>
            </td>
            <td>=</td>
            <td>
                <?=$result->add?>
            </td>
        </tr>
        <tr>
            <td>
                <b>Subtraction</b>
            </td>
            <td>
                <?=$total->num1?>
            </td>
            <td>-</td>
            <td>
                <?=$total->num2?>
            </td>
            <td>=</td>
            <td>
                <?=$total->subtract?>
            </td>
        </tr>
        <tr>
            <td>
                <b>Division</b>
            </td>
            <td>
                <?=$total->num1?>
            </td>
            <td>/</td>
            <td>
                <?=$total->num2?>
            </td>
            <td>=</td>
            <td>
                <?=$total->divide?>
            </td>
        </tr>
        <tr>
            <td>
                <b>Multiplication</b>
            </td>
            <td>
                <?=$total->num1?>
            </td>
            <td>x</td>
            <td>
                <?=$total->num2?>
            </td>
            <td>=</td>
            <td>
                <?=$total->multiply?>
            </td>
        </tr>
    </table>
    <hr />
    <b>JSON Result</b>
    <br />
    <textarea rows="10" cols="50">
        <?php
        printf("%s", trim(json_encode( (array)$total) ));
        ?>
    </textarea>
    <?php
    }
    ?>
</body>
</html>