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.

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

switch ($input3) {
    case "*":
        $input3    = "*";
        $mathinput = "Multiplication";
        $result    = $input1 * $input2;
    case "/":
        $input3    = "/";
        $mathinput = "Division";
        $result    = $input1 / $input2;
    case "+":
        $input3    = "+";
        $mathinput = "Addition";
        $result    = $input1 + $input2;
    case "-":
        $input3    = "-";
        $mathinput = "Subtraction";
        $result    = $input1 - $input2;
        $input3    = "0"; 
        $mathinput = "Invalid Operator";
if ($input3=="0"){
    echo '<div id="result"> <p>You have entered a bad operator</p> </div>' ;
    echo '<div id="result"> <p>Result: ' . $result . '</p> </div>';

1 Answer 1


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.


$input1    = $_GET["num1"];
$input2    = $_GET["num2"];
$operator  = $_GET["symbol"];

$operators = [
    '+' => 'bcadd',
    '-' => 'bcsub',
    '*' => 'bcmul',
    '/' => 'bcdiv',
    '%' => 'bcmod',

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

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


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) 
&& is_numeric($input1) 
&& 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) && is_numeric($input1) && is_numeric($input2)) {
    $var = 'all true';
else {
    $var = 'conditions not met';

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

  1. Makes coding simple if/else logic quicker
  2. Makes code shorter
  3. Makes maintaining code quicker, easier
  • \$\begingroup\$ This is interesting. I like the use of the array. Can you explain the syntax of the array_key_exists, specifically the ? before call_user_func after $input2, and the :null \$\endgroup\$
    – rskel
    Commented Feb 20, 2016 at 13:18
  • \$\begingroup\$ Thanks for the edit. I'll spend some time tonight going through this in more detail. I've read about some of the ternary logic in the Zend php course book. \$\endgroup\$
    – rskel
    Commented Feb 20, 2016 at 18:48
  • \$\begingroup\$ @JazzCat well said, well explained! \$\endgroup\$
    – Vasikos
    Commented Mar 3, 2016 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.