Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
create a PHP program that does the following:
Takes 4 numeric values
- adds the values together and outputs the answer
- subtracts the values and outputs the answer
- multiplies the values and outputs the answer
- divides the values and outputs the answer
- combination of the above and outputs the answer


What I have tried:

<form action="" method="post">
        <h1>Simple Calculator</h1><br>
        First Number:<input name="FirstNumber" value="<?php echo $FirstNumber; ?>"><br>
        Second Number:<input name="SecondNumber" value="<?php echo $SecondNumber; ?>"><br>
        Thirth Number:<input name="ThirthNumber" value="<?php echo $ThirthNumber; ?>"><br>
        Fourth Number:<input name="FourthNumber" value="<?php echo $FourthNumber; ?>"><br>

        <input type="submit" name="operator" value="-">
        <input type="submit" name="operator" value="x">
        <input type="submit" name="operator" value="+">
        <input type="submit" name="operator" value="/">
        
        <br>
        
        <br>Result: <input readonly="readonly" type='text' value="<?php echo $CalculatorResult; ?>"><br>
    </form>

<?php
$FirstNumber = isset($_POST['FirstNumber'])?$_POST['FirstNumber'] : '';
$SecondNumber = isset($_POST['SecondNumber'])?$_POST['SecondNumber']:'';
$ThirthNumber = isset($_POST['ThirthNumber'])?$_POST['ThirthNumber']:'';
$FourthNumber = isset($_POST['FourthNumber'])?$_POST['FourthNumber']:'';
$operator = isset($_POST['operator'])?$_POST['operator']:'';

$CalculatorResult = '';
if (is_numeric($FirstNumber) && is_numeric($SecondNumber)) {
    switch ($operator) {
        case "+":
        $CalculatorResult = $FirstNumber + $SecondNumber + $ThirthNumber + $FourthNumber;
        break;
        case "-":
        $CalculatorResult = $FirstNumber - $SecondNumber - $ThirthNumber - $FourthNumber;
        break;
        case "x":
        $CalculatorResult = $FirstNumber * $SecondNumber * $ThirthNumber * $FourthNumber;
        break;
        case "/":
        $CalculatorResult = $FirstNumber / $SecondNumber / $ThirthNumber / $FourthNumber;
        break;
    }
}
?>
Posted
Comments
Richard MacCutchan 24-Sep-22 11:35am    
What is the question?
Member 13746771 25-Sep-22 7:41am    
I want the combination of operations
Richard MacCutchan 25-Sep-22 9:31am    
Then you need to read PHP: Operator Precedence - Manual[^] to understand how to do it correctly.
Member 15627495 25-Sep-22 3:33am    
hello,

your project looks like a small 'calculator' program.

in 'calculator' project, you need to fill an array with 'push'
before processing calculation.

It's the only way to achieve the 'combination' you want.

to achieve :
by Js, with an array
the user write a number, then an arithmetic operator ( + - / * )
your array have to put on its stack : [ number 1 , operator 1 ...
only the button "=" will terminate this .

so for 1 + 5 * 6
you array will contains :
[1,+,5,*,6]

you read the array, and so process it, with the same case/break you wrote.


there are a lot of sample on the web, about 'calculators',
read few one :)
enjoy !
Member 13746771 25-Sep-22 7:41am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900