PHP Operators - Arithmetic operator, Assignment Operator, Increment-decrement Operator

PHP Operators - Arithmetic operator, Assignment Operator, Increment-decrement Operator

PHP Operators

To work with PHP variables and make any operations, PHP operators are used. Operators take one or more values (Operands/Variables) and yields another value. After making, we can call that an expression.

Example of Operator and Operands

<?php
   $summation = 10 + 30; 
   echo $summation; // 40
?>

In the above example -

  1. 10 = Operand
  2. 20 = Operand
  3. + = Operator

Or, this could be also done with Variable -

<?php
$number1 = 10;
$number2 = 30;
$summation = $number2 / $number1;  // Here, / is the Division operator
echo $summation; // 3
?>

In this line -

$summation = $number2 / $number1;

$number1 and $number2 are variables and / is the Division operator.

Types of Operators

According to operator values it takes, we can distinguish them below types -

  1. Arithmetic Operator
  2. Assignment Operator
  3. Increment-Decrement Operator
  4. String Operator
  5. Logical Operator
  6. Comparison Operator
  7. Conditional Operator
  8. Bitwise Operator
  9. Array Operator
  10. Type Operator
  11. Error Control Operator
  12. Execution Operator.

We’ll discuss only the First three operators in this class. Others will be discussed later, on specific part.

Arithmetic Operator

Arithmetic operators are the basic mathematic arithmetics actually. Let’s get the list of arithmetic operators -

Operator Name Example Result
+ Addition $a + $b ==> $sum = 10 + 20; // 30 Summation of $a and $b
- Subtraction $a - $b ==> $sub = 20 - 10; // 10 Subtraction of $a and $b
* Multiplication $a * $b ==> $mul = 10 * 20; // 200 Multiplication of $a and $b
/ Division $b / $a ==> $div = 20 / 10; // 2 Division of $b by $a
% Modulo $a % $b ==> $mod = 11 % 5; // 1 Reminder of $a divided by $b
** Exponentiation $a ** $b ==> $exp = 5**2; // 25 Exponentiation of $a to the power $b

Examples of Arithmetic Operator

Problem 1: Summation of Two number: Sum of 10 and 20

$number1 = 10;
$number2 = 20;

echo "Summation = " . $number1 + $number2;

Output:

Summation = 30

Problem 2: Division of Two number: 20 divide by 5

$number1 = 20;
$number2 = 5;

echo "Division = " . $number1 / $number2;

Output:

Division = 4

Problem 3: Exponentiation of Two number: 5 to the power 3

$number = 5;
$power = 3;

echo "Exponentiation = " . $number ** $power;

Output:

Exponentiation = 125

Assignment Operator

Assignment operators are used to assign value on variable.

**Example: **

<?php
$name = "Jhon"; // here = is the assignment operator.
?>

The basic assignment operator is = (equal to). It actually means that the left operand ($name) gets set to the value of the expression on the right (Jhon).
All other assignment operators are actually made from this concept. Let’s get all assignment operators:

Operator Name Example Result
= Equal to or Assign $a = 5; Assign 5 on $a variable.
+= Plus Equal (Add then Assign) $a = 10; $a += 5; Same as $a = $a + 5; ==> 15
-= Minus Equal (Subtract then Assign) $a = 10; $a -= 5; Same as $a = $a - 5; ==> 5
*= Multiplication Equal (Multiplication then Assign) $a = 10; $a *= 5; Same as $a = $a * 5; ==> 50
/= Divide Equal (Divide then Assign) $a = 10; $a /= 5; Same as $a = $a / 5; ==> 2
%= Modulo Equal (Modulus then Assign) $a = 11; $a %= 5; Same as $a = $a % 5; ==> 1

Problem 4: Summation and assign

$a = 10;
$a += 5;

echo "a = " . $a;

Output:

a = 15

Increment/Decrement Operator

Increment or Decrement operators are another kind of PHP operators which used to increase or decrease PHP variables value.

There are two types increment and decrement operator actually.

Operator Name Example Result
++ Increment $a++

$a = 5;
echo $a++; // 5
echo $a; // 6
			  
Return the value and then increment.
++$a

$a = 5;
echo ++$a; // 6
echo $a; // 6
			  
Increment the value and Return.
-- Decrement $a--

$a = 5;
echo $a--; // 5
echo $a; // 4
			  
Return the value and then decrement.
--$a

$a = 5;
echo --$a; // 4
echo $a; // 4
			  
Decrement the value and Return.

Example:

<?php
$a = 5;
echo $a++; // 5
echo $a; // 6

$b = 20;
echo ++$b; // 21
echo $b; // 21

?>

String Operator

To manipulate strings or perform any operations on strings, String operator is used.

There are two types string operator actually.

Operator Name Example Result
. Concatenation echo "Hello " . $name

$name = "Jhon";
echo "Hello " . $name;
// Hello Jhon
			  
Concat and append the string with old string.
.= Concatenation and Assign $a = "Hello "; $a .= $name

$welcome_text = "Hello";
$name = "Jhon Doe";
$welcome_text .= $name;
echo $welcome_text;
// Hello Jhon Doe
			  
Concat and assign and append the string with old string.

Example:

<?php
$name = "Jhon";
echo "Hello " . $name; // Hello Jhon

$welcome_text = "Hello ";
$name = "Jhon Doe";
$welcome_text .= $name;
echo $welcome_text; // Hello Jhon Doe

?>

Previous
Echo, PHP Statement, Print, Concat and Comments in PHP
Next
PHP Operators Part 2 - Comparison Operator, Logical Operator, Conditional Operator