Learn PHP:15-Arithmetic Operators in PHP

Posted: November 3, 2010 in PHP

PHP can perform simple mathematical operations all the way to complex trigonometric equations. The operator symbols used all make good sense for the mathematical action they perform.

Here are the arithmetic symbols and how to apply them in the most basic way:

// Set a couple of sample integer variables
$var1 = 5;
$var2 = 3;
// Addition >>> Sum of $var1 and $var2
echo $var1 + $var2;
echo "<br />";
// Subtraction >>> Difference of $var1 and $var2
echo $var1 - $var2;
echo "<br />";
// Multiplication >>> Product of $var1 and $var2
echo $var1 * $var2;
echo "<br />";
// Division >>> Quotient of $var1 and $var2
echo $var1 / $var2;
echo "<br />";
//Modulus >>> Remainder of $var1 divided by $var2
echo $var1 % $var2;
echo "<br />";
//Negation >>> Opposite of $var1
echo -$var1;

The output is shown below :


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s