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 . 

 

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.