What type do arithmetic operators (+ – / *) have in PHP? I have such situation:
$argX= "1";
$argY = "2";
$operator = "+";
I want to add up two arguments using the operator in the variable. Smth like this:
$result = $argX $operator $argY;
I know that arguments are strings, so I convert them to numbers first.
$argX = $argX+0;
$argY = $argY+0;
But in what should I convert $operator to add the arguments using a value of $operator variable? How is it possible?
No, this is not possible. You cannot use expressions for operators in PHP. Operators are operators, they don’t have a type. You’ll have to do something like this:
You could
evalit, but I wouldn’t recommend that.