Introduction to PHP Operators

Operators are essential components in any programming language, and PHP is no exception. Operators perform operations on variables and values, enabling developers to manipulate data, perform calculations, compare values, and much more.

In this blog, we’ll explore the different types of operators available in PHP, their usage, and examples to demonstrate their functionality.


Types of Operators in PHP

PHP offers several categories of operators:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Increment/Decrement Operators
  6. String Operators
  7. Array Operators
  8. Bitwise Operators
  9. Type Operators (introduced in PHP 7.0)
  10. Null Coalescing Operator (introduced in PHP 7.0)

Let’s explore each type in detail.


1. Arithmetic Operators

These operators are used to perform basic mathematical operations.

OperatorDescriptionExampleOutput
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 318
/Division15 / 35
%Modulus (Remainder)10 % 31

2. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExampleOutput
=Assign$x = 55
+=Add and Assign$x += 3$x = $x + 3
-=Subtract and Assign$x -= 2$x = $x - 2
*=Multiply and Assign$x *= 4$x = $x * 4
/=Divide and Assign$x /= 2$x = $x / 2
%=Modulus and Assign$x %= 3$x = $x % 3

3. Comparison Operators

These operators compare two values and return true or false.

OperatorDescriptionExampleOutput
==Equal to5 == 5true
!=Not equal to5 != 3true
<Less than3 < 5true
>Greater than5 > 3true
<=Less than or equal to3 <= 3true
>=Greater than or equal to5 >= 3true
===Identical (Equal + Type)5 === '5'false
!==Not identical5 !== '5'true

4. Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExampleOutput
&&Logical AND(true && false)false
``Logical OR
!Logical NOT!(true)false

5. Increment/Decrement Operators

These operators increase or decrease the value of a variable.

OperatorDescriptionExampleOutput
++$xPre-increment++$xIncrements before use
$x++Post-increment$x++Increments after use
--$xPre-decrement--$xDecrements before use
$x--Post-decrement$x--Decrements after use

6. String Operators

String operators are used to manipulate strings.

OperatorDescriptionExampleOutput
.Concatenation"Hello" . "World"HelloWorld
.=Concatenation and Assign$x .= "World"Appends "World" to $x

7. Array Operators

Array operators compare arrays or perform operations on them.

OperatorDescriptionExampleOutput
+Union of arrays[1, 2] + [3, 4][1, 2, 3, 4]
==Equal arrays[1, 2] == [2, 1]false
===Identical arrays[1, 2] === [1, 2]true

8. Bitwise Operators

Bitwise operators perform operations on bits.

OperatorDescriptionExampleOutput
&Bitwise AND5 & 31
``Bitwise OR`5
^Bitwise XOR5 ^ 36
~Bitwise NOT~5-6

9. Null Coalescing Operator

Introduced in PHP 7.0, this operator returns the first operand if it exists and is not null. Otherwise, it returns the second operand.

$name = null; echo $name ?? "Guest"; // Outputs: Guest

Conclusion

PHP offers a wide variety of operators to handle diverse programming needs, from arithmetic calculations to logical comparisons. Understanding how these operators work is essential for writing efficient and error-free code.

Start experimenting with these operators in your PHP projects to master their functionality and make your applications more robust.