A
A
alex88a2017-02-18 00:07:19
PHP
alex88a, 2017-02-18 00:07:19

How to put an unknown mathematical sign between two arrays?

there is a code like this <?php echo $a + $b ?> which outputs addition. now you need to do what would be displayed not +, but what is in the array $ c. there can be +, -, *, /.
how to make it so that instead of the + sign, an unknown mathematical sign, aka $c , is displayed?
thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Immortal_pony, 2017-02-18
@alex88a

function doAction($a, $b, $action) {
     switch ($action) {
          case "+": return $a + $b;
          case "-": return $a - $b;
          case "*": return $a * $b;
          case "/": return $a / $b;
          default: throw new \BadFunctionCallException("Unknown action");
    }
}

Well, or use eval

M
McBernar, 2017-02-18
@McBernar

Hm) Cool question.
Only the function with the switch came to mind, like Immortal_pony.

X
xmoonlight, 2017-02-18
@xmoonlight

https://wiki.php.net/rfc/operator-overloading
Through class creation and operator overloading!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question