What are PHP Operators, PHP Operator types

What are PHP Operators, PHP Operator types

What are PHP Operators?

Operators are sings or symbols that are used to perform operations with variables and values. Like: +, -, =, ==, > etc.

Example: –

<?php
$x = 5;
$y = 4;
echo $x + $y;
?>

In the above example + plus is an operator and it is used to perform additions.

Browser Output
9

PHP Operator types

In PHP, there are so many operators, PHP divides these operators into the following groups –

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Incrementing and Decrementing Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Spaceship Operator (Introduced in PHP 7)

This is a list of the most used operators. But in PHP, there are also more operators. Check out the list of all operators.


In this chapter, we will only discuss Arithmetic and Assignment operators. But in the next tutorials, we will discuss other operators briefly.

PHP Arithmetic Operators

The PHP Arithmetic Operators are used to perform common math operations, such as addition, subtraction, multiplication, etc. You can also call these, mathematical operators.

$x = 6;
$y = 2;

OperatorNameExampleResult
+Addition
$x + $y
8
Subtraction
$x - $y
4
*Multiplication
$x * $y
12
/Division
$x / $y
3
%Modulus
$x % $y
0
**Exponentiation
$x ** $y (62)
36

PHP Assignment Operator

PHP Assignment Operator is used to assign value to a variable. The assignment operator is the equal sign =.

<?php
$x = "Hello PHP";
echo "$x";
?>
Browser Output
Hello PHP

PHP Combined Assignment Operators

Combined Assignment Operators are also used to assign values to variables.

But combined assignment operators first perform operations then assign values to variables.

<?php
$x = 6;
$x += 2;
/*
Same as
$x = $x + 2;
*/
echo $x;
?>
Browser Output
8

In the above of example, first, we declare the $x variable with value 6, then in the second line, first, perform the addition operation $x + 2 and then we reassign the addition result into the $x variable.

List of PHP Combined Assignment Operators –

$x = 6;

OperatorExampleSame asResult
+=
$x += 5
$x = $x + 5
11
-=
$x -= 2
$x = $x - 2
4
*=
$x *= 3
$x = $x * 3
18
/=
$x /= 2
$x = $x / 2
3
%=
$x %= 3
$x = $x % 3
0
**=
$x **= 2
$x = $x ** 2
36

Example:-

<?php
$x = 6;
$x += 5;
echo "Addition: $x <br>";

$x = 6;
$x -= 2;
echo "Subtraction: $x <br>";

$x = 6;
$x *= 3;
echo "Multiplication: $x <br>";

$x = 6;
$x /= 2;
echo "Division: $x <br>";

$x = 6;
$x %= 3;
echo "Modulus: $x <br>";

$x = 6;
$x **= 2;
echo "Exponentiation: $x";
?>
Browser Output
Addition: 11
Subtraction: 4
Multiplication: 18
Division: 3
Modulus: 0
Exponentiation: 36

Next is – PHP if, if else and if elseif else Statement

Previous isPHP Comments

Posts Navigation


Leave a Reply

Your email address will not be published. Required fields are marked *