0

//lets think that we have two input variables $a and $b ..and those are numbers.. //passing into the function.

<?php

  function calculation($a,$b)
    {
      echo $a+$b;
    }
   //calling function

   calculation(7,3);


    //then we get the output as 10

    ?>

//but how to pass "+" to the function

   <?php
    function calculation($a,$b,$symbol)
    {
        echo $a."$symbol".$b;  
    }

     calculation(7,3,"+");
    ?>
  then it gives output as  "7+3"

  i know that i am just doing concardination 

how to pass " + " for calculation purpouse

Pim
  • 850
  • 7
  • 17
aardhan
  • 11
  • Normally a bad idea if not done properly, but look into [eval](http://php.net/manual/en/function.eval.php) – aynber Sep 25 '17 at 14:18
  • As said by aynber it can be achieved by using eval (as stated in the duplicated question i added) but be very careful, eval does provide a great amount of security vunerability if youre not ultra-careful. – Fabian S. Sep 25 '17 at 14:19
  • 1
    Don't use `eval` use a switch case instead in your calculation function, it's the safest solution. – teeyo Sep 25 '17 at 14:19
  • 1
    I agree with @teeyo. You should simply use a switch case, switch for the symbol and do the math that way. So many less security vulnerabilities. But also, just writing the calculation normally is way easier to type, it's less characters. `echo 7+3` – GrumpyCrouton Sep 25 '17 at 14:25

0 Answers0