Assignment operator as a variable in php -


i trying use operator variable.

$num1 = 33; $num2 = 44; $operator = "+"; $answer = ($num1.$operator.$num2); 

it prints "33+44" instead of 77.

or,

$operators = array(    "plus"=>"+",     "minus"=>"-",    "times"=>"*",     "div"=>"/"   ); 

i want able use as: $answer = $num1.$operators["plus"].$num2;

how can achieve without using "if" statement?.

$answer = ($operator == "+") ? $num1 + $num2 : 0; 

    $num1 = 33;     $num2 = 44;     $operator = "+";     eval("\$result = $num1 $operator $num2;");      echo $result; 

caution eval() language construct dangerous because allows execution of arbitrary php code. use discouraged. if have verified there no other option use construct, pay special attention not pass user provided data without validating beforehand.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -