Operators are symbols or keywords that perform specific operations on values or variables. PHP provides a wide range of operators for arithmetic, assignment, comparison, logical operations, and more. In this section, we'll explore the different types of operators available in PHP.

 

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. Here are the arithmetic operators in PHP:

 

Operator Name Example Result
+ Addition 3 + 4 7
- Subtraction 10 - 3 7
* Multiplication 5 * 6 30
/ Division 20 / 4 5
% Modulus (remainder) 15 % 4 3
** Exponentiation 2 ** 3 8

 

Example:

 $x = 10;
$y = 3;
$sum = $x + $y;      // $sum will be 13
$diff = $x - $y;     // $diff will be 7
$product = $x * $y;  // $product will be 30
$quotient = $x / $y; // $quotient will be 3.3333333333333
$remainder = $x % $y; // $remainder will be 1
$power = $x ** $y;   // $power will be 1000

Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator is =, but PHP also provides compound assignment operators that combine an arithmetic operation with assignment.

Operator Example Equivalent
= $x = 5 $x = 5
+= $x += 3 $x = $x + 3
-= $x -= 2 $x = $x - 2
*= $x *= 4 $x = $x * 4
/= $x /= 2 $x = $x / 2
%= $x %= 3 $x = $x % 3

 

Example:

$x = 10;
$x += 5; // $x is now 15
$x -= 3; // $x is now 12
$x *= 2; // $x is now 24

Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true or false). These operators are often used in conditional statements and loops.

Operator Name Example Result
== Equal 5 == 5 true
!= Not equal 7 != 3 true
> Greater than 10 > 5 true
< Less than 3 < 7 true
>= Greater than or equal to 8 >= 8 true
<= Less than or equal to 4 <= 9 true

 

Example:

$x = 10;
$y = 5;

if ($x > $y) {
    echo "X is greater than Y";
} else {
    echo "X is not greater than Y";
}

Logical Operators

Logical operators are used to combine or negate boolean expressions. They are often used in control structures and conditional statements.

Operator Name Description
&& And Returns true if both operands are true
|| Or Returns true if at least one operand is true
! Not Negates the boolean value of the operand
and And Same as &&, but with lower precedence
or Or Same as ||, but with lower precedence
xor Exclusive Or Returns true if exactly one operand is true

 

Example:

$x = 10;
$y = 5;
$z = 8;

if ($x > $y && $x > $z) {
    echo "X is the largest";
} elseif ($y > $x || $y > $z) {
    echo "Y is the largest";
} else {
    echo "Z is the largest";
}
 

String Operators

String operators are used to manipulate and concatenate strings.

Operator Name Example Result
. Concatenation "Hello " . "World" "Hello World"
.= Concatenation assignment $greeting .= "World" $greeting becomes "Hello World"

 

Example:

$name = "Alice";
$greeting = "Hello, " . $name; // $greeting is "Hello, Alice"
$greeting .= "!"; // $greeting is now "Hello, Alice!"
 

Increment/Decrement Operators

The increment and decrement operators are used to increase or decrease the value of a variable by 1.

Operator Name Description
++ Pre-increment Increments the value by 1, then returns the incremented value
++ Post-increment Returns the value, then increments it by 1
-- Pre-decrement Decrements the value by 1, then returns the decremented value
-- Post-decrement Returns the value, then decrements it by 1

 

Example:

$x = 5;
echo ++$x; // Outputs 6, $x is now 6
echo $x++; // Outputs 6, $x is now 7

$y = 10;
echo --$y; // Outputs 9, $y is now 9
echo $y--; // Outputs 9, $y is now 8
 

Other Operators

PHP also provides several other operators, including:

  • Array Operators: Used for working with arrays, such as + (union), == (equality), === (strict equality), and more.
  • Conditional Operator: The ternary operator ? : is a shorthand for simple if...else statements.
  • Execution Operator: The backtick operator ` is used to execute external programs and get their output.
  • Error Control Operator: The @ operator suppresses error messages from expressions.
  • Null Coalescing Operator: The ?? operator provides a way to use a default value if a variable is null.

In the next section, we'll explore control structures in PHP, which allow you to control the flow of execution in your code.