I am trying to use an operator as a 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 to be able to use as: $answer = $num1.$operators["plus"].$num2;
How can I achieve this without using "if" statement?.
$answer = ($operator == "+") ? $num1 + $num2 : 0;
You can try to use BC Math functions. This example works with php 5.5:
$num1 = 33;
$num2 = 44;
$operator = '+';
$funcs = array(
'+' => 'bcadd',
'-' => 'bcsub',
'/' => 'bcdiv',
'*' => 'bcmul',
);
$answer = $funcs[$operator]($num1, $num2); // 77
$num1 = 33;
$num2 = 44;
$operator = "+";
eval("\$result = $num1 $operator $num2;");
echo $result;
Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.
On the user-land
Think about things that you're trying to achieve. Why don't create fixed-code closures? You may easily do it with:
$operators = array(
"plus"=>function($x, $y)
{
return $x+$y;
},
"minus"=>function($x, $y)
{
return $x-$y;
},
"times"=>function($x, $y)
{
return $x*$y;
},
"div"=>function($x, $y)
{
return $x/$y;
}
);
//in PHP 5.5:
$result = $operators['plus']($num1, $num2);
//in PHP<5.5:
//$result = call_user_func_array($operators['plus'], [$num1, $num2])
Now you have two things: first, it's still dynamic code - and you may use it in expressions. Next, you're safe in terms of code evaluation - because you've defined the code by yourself. This approach also has advantage - you may define desired behavior as you wish. For instance, raise an exception for "div"
operator if second operand is zero.
"Easy" way
I'll notice this only to explain why this isn't a right way. You may use eval()
. It's like:
eval('$result='.$num1.$operators['plus'].$num2.';');
And then your result would be stored in $result
variable. It is about "easy" way, but not "right" way. The problem is - it is unsafe. Just think about it - you're executing some code, which is dynamic. If your code has nothing to do with user input, it may be applicable - in case, if you're checking your operands and operator before. But I still recommend to think twice before applying it.
If evaluation still needed
Then I recommend to look to some existing implementations. Here are some links:
I question why you want to pass the arithmetic operator as a string in the first place, but here's a way to do it without using an if statement:
function fancy_math($num1 = 0, $num2 = 0, $operator = '+')
{
switch ($operator) {
case 'minus':
case '-':
return $num1 - $num2;
break;
case 'times':
case '*':
return $num1 * $num2;
break;
case 'div':
case '/':
return ($num2 !== 0) ? $num1 / $num2 : 'Error: divide by zero';
break;
default:
return $num1 + $num2;
break;
}
}
And then call it like this:
$answer = fancy_math($num1, $num2, $operator);
Note you can pass 'minus'
or '-'
(etc) as the $operator
as it will accept both. If you don't pass an $operator
it will assume 'plus'
or '+'
automatically.