Arithmetic Operators in PHP: A Comprehensive Guide


In PHP, arithmetic operators are used to perform mathematical operations on numerical values. These operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%), as well as increment (++) and decrement (--) operators.

Operator Name Description
+$a Identity Conversion of $a to int or float as appropriate
-$a Negation Opposite of $a
$a + $b Addition Sum of $a and $b
$a - $b Subtraction Difference of $a and $b
$a * $b Multiplication Product of $a and $b
$a / $b Division Quotient of $a and $b
$a % $b Modulo Remainder of $a divided by $b
$a ** $b Exponentiation Result of raising $a to the $b'th power

Operator precedence determines the order in which the arithmetic operations are evaluated. For example, multiplication and division have a higher precedence than addition and subtraction. To change the order of operations, parentheses can be used.

Overall, arithmetic operators in PHP are essential for performing mathematical operations and manipulating numerical data within PHP programs.

Sample Code

<?php 
//Arithmetic Operators

//Identity
$a="10";
echo "<br>".gettype($a);
$a=+"10";
echo "<br>".gettype($a);
//Negation
$a=40;
echo "<br>A Value : ".$a;
echo "<br>A Value : ".-$a;
//Addition
$a=123;
$b=10;
echo "<br> Total : ".$a+$b;
//Subtraction
echo "<br> Diffrence : ".$a-$b;
//Multiplication
echo "<br> Multiply : ".$a*$b;
//Division
echo "<br> Division : ".$a/$b;
//Modulo
echo "<br> Modulo : ".$a%$b;
//Exponentiation
$base=2;  //2*2*2*2
$power=4;
echo "<br> Exponentiation : ".$base**$power;
?>

This code demonstrates the use of arithmetic operators in PHP to perform mathematical operations. Here is a breakdown of each section of the code:

  • Identity: The "+" operator can be used as a shorthand for converting a string to a number in PHP. In this case, the variable $a is initially assigned a value of "10" as a string, but using the unary plus operator converts it to an integer value of 10.
  • Negation: The "-" operator can be used to negate a numerical value. In this example, the variable $a is assigned a value of 40, and then the negation operator is used to output the negative value of -40.
  • Addition: The "+" operator can be used to add two numerical values together. In this example, the variables $a and $b are added together using the "+" operator to output the total.
  • Subtraction: The "-" operator can be used to subtract one numerical value from another. In this example, the variable $b is subtracted from $a using the "-" operator to output the difference.
  • Multiplication: The "" operator can be used to multiply two numerical values. In this example, the variables $a and $b are multiplied together using the "" operator to output the result.
  • Division: The "/" operator can be used to divide one numerical value by another. In this example, the variable $a is divided by $b using the "/" operator to output the result.
  • Modulo: The "%" operator can be used to find the remainder of a division operation. In this example, the variable $a is divided by $b using the "%" operator to output the remainder.
  • Exponentiation: The "" operator can be used to raise a base value to a power. In this example, the variable $base is raised to the power of $power using the "" operator to output the result.

Overall, this code provides a good introduction to the different arithmetic operators in PHP and how they can be used to perform mathematical operations on numerical values.