Tuesday, October 20, 2009

The Math Operators








The Math Operators


We'll start with the math operators, which are the most basic of all operators:


+

Adds two numbers

-

Subtracts one number from another

*

Multiplies two numbers together

/

Divides one number by another

%

Returns the remainder when one number is divided by another (modulus).



You can see them all at work in phpmath.php; here's the PHP:


Example 2-1. Using the Math operators, phpmath.php



<HTML>
<BODY>
<H1>The math operators</H1>
<?php
echo "5 + 2 = ", 5 + 2, "<BR>";
echo "5 - 2 = ", 5 - 2, "<BR>";
echo "5 * 2 = ", 5 * 2, "<BR>";
echo "5 / 2 = ", 5 / 2, "<BR>";
echo "5 % 2 = ", 5 % 2, "<BR>";
?>
</BODY>
</HTML>



The results, all as expected, appear in Figure 2-1.


Figure 2-1. The math operators.

[View full size image]









    No comments:

    Post a Comment