EZ

Eduzan

Learning Hub

Eduzan
Eduzan / PHP

PHP Operators

Computer Science / PHP tutorial chapter - Published 2025-12-16 - PHP

Operators are fundamental tools in any programming language, and PHP is no exception. They allow us to perform various operations on values or variables. To put it simply, operators take values (or operands), apply a specified operation, and return a result. For instance, in the expression “5 + 3 = 8,” the + symbol is an operator. It takes the values 5 and 3, adds them, and produces 8.

PHP provides several types of operators to handle different operations, such as arithmetic operations (e.g., addition, subtraction), logical operations (e.g., AND, OR), increment/decrement operations, and more. These operators are represented by symbols, and they help in manipulating variables or values efficiently. Below is a breakdown of the different groups of operators available in PHP:

  • Arithmetic Operators
  • Logical or Relational Operators
  • Comparison Operators
  • Conditional or Ternary Operators
  • Assignment Operators
  • Spaceship Operators (Introduced in PHP 7)
  • Array Operators
  • Increment/Decrement Operators
  • String Operators

1. Arithmetic Operators: Arithmetic operators are used to perform basic mathematical calculations, such as addition, subtraction, multiplication, and so on. The table below lists the common arithmetic operators in PHP, along with their syntax and operations.

PHP Operators
OperatorNameSyntaxOperation
+Addition$a + $bAdds two operands
-Subtraction$a - $bSubtracts second operand from first
*Multiplication$a * $bMultiplies two operands
/Division$a / $bDivides first operand by second
**Exponentiation$a ** $bRaises $a to the power of $b
%Modulus$a % $bReturns the remainder of division

Example:

<?php
$a = 15;
$b = 4;

echo "Addition: " . ($a + $b) . "\n";
echo "Subtraction: " . ($a - $b) . "\n";
echo "Multiplication: " . ($a * $b) . "\n";
echo "Division: " . ($a / $b) . "\n";
echo "Exponentiation: " . ($a ** $b) . "\n";
echo "Modulus: " . ($a % $b) . "\n";
?>

Output:

Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Exponentiation: 50625
Modulus: 3

2. Logical or Relational Operators: These operators are primarily used with conditional expressions to check whether certain conditions are met (i.e., whether they are true or false). Logical operators are key in making decisions within the code.

PHP Operators
OperatorNameSyntaxOperation
andLogical AND$a and $bTrue if both $a and $b are true
orLogical OR$a or $bTrue if either $a or $b is true
xorLogical XOR$a xor $bTrue if only one operand is true, but not both
&&Logical AND$a && $bTrue if both $a and $b are true (same as and)
` `Logical OR
!Logical NOT!$aTrue if $a is false

Example:

<?php
$a = 40;
$b = 25;

if ($a == 40 and $b == 25)
    echo "and Success\n";

if ($a == 40 or $b == 20)
    echo "or Success\n";

if ($a == 40 xor $b == 30)
    echo "xor Success\n";

if ($a == 40 && $b == 25)
    echo "&& Success\n";

if ($a == 40 || $b == 20)
    echo "|| Success\n";

if (!$b)
    echo "! Success\n";
?>

Output:

and Success
or Success
xor Success
&& Success
|| Success

3. Comparison Operators: Comparison operators compare two values and return a boolean (true or false) based on whether the condition is satisfied.

PHP Operators
OperatorNameSyntaxOperation
==Equal$a == $bReturns true if $a equals $b
!=Not equal$a != $bReturns true if $a does not equal $b
<Less than$a < $bReturns true if $a is less than $b
>Greater than$a > $bReturns true if $a is greater than $b
<=Less or equal$a <= $bReturns true if $a is less than or equal to $b
>=Greater or equal$a >= $bReturns true if $a is greater than or equal to $b

Example:

<?php
$x = 70;
$y = 30;

var_dump($x == $y);
var_dump($x != $y);
var_dump($x < $y);
var_dump($x > $y);
?>

Output:

bool(false)
bool(true)
bool(false)
bool(true)
End of lesson.