4

In PHP, I can do this:

$value1 = 5;
$value2 = -2;
echo $value1 + $value2; // 3

But how would I do this with multiplication or division? Something like:

$value1 = 10;
$value2 = /2;
echo $value1 (?) $value2; // 5;

How would I manage this situation as simply as possible?

Frantisek
  • 7,485
  • 15
  • 59
  • 102
  • are you asking if you can add the operator to a string and evaluate as math? as you put it here, /2 is invalid. It will throw an error – Kai Qing Nov 22 '12 at 01:06
  • I know it will. I'm asking how to do this properly, without an error. – Frantisek Nov 22 '12 at 01:06
  • fair enough, is the question then how to add the opoerator as a string within the value of a variable or are you just looking for a yes or no it can't be done this way. Cause I think succinctly the answer is no. Though the bigger question may be why do you want to do it this way – Kai Qing Nov 22 '12 at 01:10

3 Answers3

5

If you only need to differentiate between division and multiplication,

$value2 = 2;
//or
$value2 = 1/2;

echo $value1 * $value2;

Your code works with addition and subtraction, because -2 in $value2 = -2; does not mean "subtract two". It means "[add] minus two". For multiplication, you need "two" or "the inverse of two"

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
  • This seems like the most reasonable solution, thanks! Vdaka! :) – Frantisek Nov 22 '12 at 01:12
  • 1
    it should be noted that $value2 = 1/2 is actually doing the math. it is dividing 1 by 2. You could replace 1/2 with .5 to avoid the minuscule unnecessary processing. Just saying. This is a good solution as is in general use – Kai Qing Nov 22 '12 at 01:13
  • 1
    I don't think saying .5 is the proper solution. The answer works generally for any number I'd want to input, for example 1/400, or whatever. It should stay as it is. – Frantisek Nov 22 '12 at 01:15
  • 2
    @KaiQing There is no exact decimal representation of 1/3. A compiler should optimise '1/2` anyways. There ARE true compilers for PHP. – John Dvorak Nov 22 '12 at 01:15
  • yeah but you can come close depending on your needs. Like I said, the answer as is works as needed. All I am pointing out is that it is not like saying $value2 = half. It is saying $value2 = divide one by two. – Kai Qing Nov 22 '12 at 01:18
3

In a word, no.

In a paragraph, you could create an anonymous function to capture the meaning of your $value2:

$value1 = 5;
$op_and_value2 = function($value) {
  return $value1 / 2;
};

echo $op_and_value2($value1); # 2

Or you could make a class to encapsulate this behaviour, but that's even more work.

Or you can go to the dark side, and use eval.

$value1 = 5;
$value2 = "/ 2";
echo eval("return $value1 $value2;"); # 2

(If "dark side" wasn't hint enough, don't do this unless you want everyone to hate you.)

A better approach all around would be to store operator and value2 separately (although, you can still put them into a structure together); the operator would be best stored as a function (perhaps an anonymous function like above, but with two arguments, and not a hard-coded 2).

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • This sort of makes me feel good that I am not the only one who suggested `eval`... – Aamir Nov 22 '12 at 01:13
  • @aam1r: Haha... well, as a literal answer to the question, it just pops into one's mind. But I do feel dirty just for mentioning it. – Amadan Nov 22 '12 at 01:14
2

Yes, but it is an unpleasant way of doing this:

You can use eval():

$value1 = 10;
$value2 = "/2";
echo eval("return $value1 $value2;"); // 5;

I would be very cautious in using eval() in code running in production though. If you end up using this approach, I would suggest reading these 2 discussions:

Community
  • 1
  • 1
Aamir
  • 5,324
  • 2
  • 30
  • 47