Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, can I store division or multiplication along with a number?

Tags:

php

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?

like image 638
Frantisek Avatar asked Dec 26 '22 14:12

Frantisek


1 Answers

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"

like image 127
John Dvorak Avatar answered Feb 22 '23 22:02

John Dvorak