PHP Arithmetic Operators

In this chapter you will learn:

  1. What are arithmetic operators
  2. PHP Arithmetic Operators

Description

The arithmetic operators handle basic numerical operations, such as addition and multiplication.

PHP Arithmetic Operators

The full list arithmetic operators in PHP is shown in the following table.

Operator MeaningOperation
+ Addition Returns the first value added to the second: $a + $b.
- Subtraction Returned the second value subtracted from the first: $a - $b.
* Multiplication Returns the first value multiplied by the second: $a * $b.
/ Division Returns the first value divided by the second: $a / $b.
% ModulusDivides the first value into the second, then returns the remainder: $a % $b. This only works on integers, and the result will be negative if $a is negative.
+= Shorthand addition Adds the second value to the first: $a += $b. Equivalent to $a = $a + $b.
-= Shorthand subtraction Subtracts the second value from the first: $a -= $b. Equivalent to $a = $a - $b.
*= Shorthand multiplication Multiplies the first value by the second: $a *= $b. Equivalent to $a = $a * $b.
/= Shorthand division Divides the first value into the second: $a /= $b. Equivalent to $a = $a / $b.

An exponentiation is done via the pow() function. Here are some examples.


<?PHP//  ja v  a  2  s  .  com
      $a = 13; 
      $b = 4; 
      $c = 3.33; 
      $d = 3.99999999; 
      $e = -10; 
      $f = -4; 

      print $a + $b; 
      print "\n"; 
      print $a - $c; 
      print "\n"; 
      print $a * $d; 
      print "\n"; 
      print $a / $f; 
      print "\n"; 
      print $e % $b; 
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What are incrementing and decrementing operators
  2. Difference between incrementing and decrementing operators
Home » PHP Tutorial » PHP Operators
PHP Assignment Operators
PHP Arithmetic Operators
PHP Incrementing and Decrementing Operators
PHP Comparison Operators
PHP Logical Operators
PHP String Operators
PHP Ternary Operator
PHP Bitwise Operators
PHP Execution Operator
PHP Operator Precedence