Jump to content

super easy problem involving simple math functions


silverglade

Recommended Posts

I have no clue what is going on. It is driving me insane so I am trying the forum. The below code is self explanatory, and I posted my sample output after the code. Any help greatly appreciated. thank you.

 

 

here is the code

 

 

 

<?php	

       echo "<br /> Now for some math!!<br />";
   $int1 = 2;
   $int2 = 3;



        echo "<br /> \$int2 + \$int1 = ".$int2 + $int1. " .";

//subtracting

echo "<br /> \$int2 -  \$int1 = ".$int2 - $int1. " .";

//multiply

echo " <br /> \$int1 X \$int2 = ".$int1 * $int2." .";

//divide

echo "<br /> \$int2 divided / by \$int1 = ".$int2 / $int1. " .";

//remainder or modulus operator

echo "<br /> \$int2 remainder of \$int1 = ". $int2 % $int1. " .";
       ?>

       Sample output is this:

Now for some math!!
2 .-2 .
$int1 X $int2 = 6 .
$int2 divided / by $int1 = 1.5 .
$int2 remainder of $int1 = 1 . 

 

 

Enclose the math operations in parentheses to force the calculation to be performed before the value is echoed.

 

<?php
echo "<br /> Now for some math!!<br />";
$int1 = 2;
$int2 = 3;
echo "<br /> \$int2 + \$int1 = " . ($int2 + $int1);

//subtracting
echo "<br /> \$int2 -  \$int1 = " . ($int2 - $int1);

//multiply
echo " <br /> \$int1 X \$int2 = " . ($int1 * $int2);

//divide
echo "<br /> \$int2 divided / by \$int1 = " . ($int2 / $int1);

//remainder or modulus operator
echo "<br /> \$int2 remainder of \$int1 = " . ($int2 % $int1);
?>

thank you so much spiderwell and Pikachu2000. This PHP for dummies book is not too good. In my book they were not inside parenthesis. It was strange that it worked for some without the parenthesis, but with + and - it didn't . So when I added () to the + and - terms, it worked!! You both  preserved my SANITY!! LOL. so thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.