This question already has an answer here:
I have probably very stupid question, but I have this code:
<?php
$x=2;
$y=4;
print ("iloczyn = ".$x." * ".$y." = ".$x*$y. "<br>");
print ("iloraz = ".$x." / ".$y." = ".$x/$y. "<br>");
print ("suma = ".$x." + ".$y." = ".$x+$y. "<br>");
print ("roznica = ".$x." - ".$y." = ".$x-$y. "<br>");
?>
And it is not working. The first two, multiplication and division are fine. But addition and subtraction are not ok. Result of this script is like that:
iloczyn = 2 * 4 = 8
iloraz = 2 / 4 = 0.5
4
-4
Any idea why? Thanks in advance!
</div>
Parenthesis save lives:
$x=2;
$y=4;
print ("iloczyn = ".$x." * ".$y." = ".($x*$y). "<br>");
print ("iloraz = ".$x." / ".$y." = ".($x/$y). "<br>");
print ("suma = ".$x." + ".$y." = ".($x+$y). "<br>");
print ("roznica = ".$x." - ".$y." = ".($x-$y). "<br>");
You're concatenating, so it converts them to a string. Put parens around and it will work fine.
$x= 2;
$y= 4;
print ("iloczyn = ".$x." * ".$y." = ".($x*$y). "<br>");
print ("iloraz = ".$x." / ".$y." = ".($x/$y). "<br>");
print ("suma = ".$x." + ".$y." = ".($x+$y). "<br>");
print ("roznica = ".$x." - ".$y." = ".($x-$y). "<br>");