Beginner PHP calculator I've spent the last few evenings learning PHP, CSS and HTML through Codecademy and now on Team Treehouse. My goal is to progress with PHP over the next year or two and perhaps obtain Zend cert.

Anyway, I've worked on a simple calculator to test what I've learned. I'd like to see, (given the same 'functionality')what more experienced programmers would come up with.  I think this would enhance my learning curve. 

I've used a switch statement, because I'm not sure if it's possible to do something like this:

Variable = Math Operator

Var1 = User Input
Var2 = User Input

Variable3 = Var1 Variable Var2


Example, if you entered *, 2, 2, Variable 3 would = 4.

I tried a few ways but could not make it work, so had to settle with a switch which seems cumbersome.

&lt;?php
$input1    = $_GET["num1"];
$input2    = $_GET["num2"];
$input3    = $_GET["symbol"];
$mathinput = "Not Selected";


switch ($input3) {
    case "*":
        $input3    = "*";
        $mathinput = "Multiplication";
        $result    = $input1 * $input2;
        break;
    case "/":
        $input3    = "/";
        $mathinput = "Division";
        $result    = $input1 / $input2;
        break;
    case "+":
        $input3    = "+";
        $mathinput = "Addition";
        $result    = $input1 + $input2;
        break;
    case "-":
        $input3    = "-";
        $mathinput = "Subtraction";
        $result    = $input1 - $input2;
        break;
    default:
        $input3    = "0"; 
        $mathinput = "Invalid Operator";
}
if ($input3=="0"){
    echo '&lt;div id="result"&gt; &lt;p&gt;You have entered a bad operator&lt;/p&gt; &lt;/div&gt;' ;
}
else{
    echo '&lt;div id="result"&gt; &lt;p&gt;Result: ' . $result . '&lt;/p&gt; &lt;/div&gt;';
}  
?&gt;
No it's not possible writing 

Variable3 = Var1 Variable Var2


With the Variable as an operator, i decided to use call_user_func to call the BC Math functions http://php.net/manual/en/ref.bc.php

I removed the $mathinput since you were not using it anyway.


  Result


&lt;?php
$input1    = $_GET["num1"];
$input2    = $_GET["num2"];
$operator  = $_GET["symbol"];

$operators = [
    '+' =&gt; 'bcadd',
    '-' =&gt; 'bcsub',
    '*' =&gt; 'bcmul',
    '/' =&gt; 'bcdiv',
    '%' =&gt; 'bcmod',
];

$result = array_key_exists($operator, $operators) 
        &amp;&amp; is_numeric($input1) 
        &amp;&amp; is_numeric($input2) 
        ? call_user_func($operators[$operator], $input1, $input2) 
        : null;
?&gt;      

&lt;?php if (!$result) : ?&gt;
        &lt;div id="result"&gt;
            &lt;p&gt;Error: Invalid input or arithmetic operator.&lt;/p&gt; 
        &lt;/div&gt;
&lt;?php else: ?&gt;
        &lt;div id="result"&gt;
            &lt;p&gt;Result: &lt;?= $result; ?&gt;&lt;/p&gt; 
        &lt;/div&gt;
&lt;?php endif; ?&gt;



  EDIT


The array_key_exists function checks if a given key exists in the array.
http://php.net/manual/en/function.array-key-exists.php

The following is a shorthand if / ternary logic

$var = array_key_exists($operator, $operators) 
&amp;&amp; is_numeric($input1) 
&amp;&amp; is_numeric($input2)
? 'all true' : 'conditions not met';


Simply checks if all the conditions are true, if so the above example would set $var to a string with a value of 'all true', if one of the conditions would return false, the $var would be set to 'conditions not met'

You could write the above snippet using regular if / else statements like this:

if(array_key_exists($operator, $operators) &amp;&amp; is_numeric($input1) &amp;&amp; is_numeric($input2)) {
    $var = 'all true';
}
else {
    $var = 'conditions not met';
}


Using ternary logic could have some of the following advantages when used properly.


Makes coding simple if/else logic quicker
Makes code shorter 
Makes maintaining code quicker, easier