Table of contents
1.
Introduction
2.
What are the operators in PHP?
3.
Arithmetic Operators
4.
Assignment Operators
5.
Bitwise Operators
6.
Comparison Operators
7.
Incrementing/Decrementing Operators
8.
Logical Operators
9.
String Operators
10.
Array Operators
11.
Execution Operators
12.
Error Control Operators
13.
Conditional Assignment Operators
14.
Frequently Asked Questions
14.1.
What is '@' in PHP?
14.2.
Why do we use :: in PHP?
14.3.
What is the operator and type?
15.
Conclusion
Last Updated: Jan 13, 2025
Easy

Understanding PHP Operators

Author Abhay Trivedi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

PHP Operators are symbols that we use to perform operations on operands. We can classify PHP operators on behalf of operands into three types:

  • Unary Operators - It works on single operands. For eg. ++, -- etc.
  • Binary Operators - It works on two operands. For eg. binary +, -, *, / etc.
  • Ternary Operators - It works on three operands. For eg. "?:".
Understanding PHP Operators

What are the operators in PHP?

Operators in PHP are symbols or keywords used to perform operations on variables and values. They are categorized based on their functionality.

We can classify PHP Operators in the following forms:-

  • Arithmetic Operators
  • Assignment Operators
  • Bitwise Operators
  • Comparison Operators
  • Incrementing/Decrementing Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Type Operators
  • Execution Operators
  • Error Control Operators

Arithmetic Operators

We use Arithmetic Operators to perform arithmetic procedures such as addition, subtraction, multiplication, etc.

OperatorNameSyntaxExplanation
Addition (+)Sum$a + $bAdds the operands.
Subtraction (-)Difference$a - $bSubtracts operand $b from operand $a.
Multiplication (*)Product$a * $bMultiplies the operands.
Division (/)Quotient$a / $bDivides operand $a by operand $b.
Modulus (%)Remainder$a % $bReturns the remainder of division.
Exponentiation ()**Power$a ** $bRaises $a to the power of $b.

Assignment Operators

We use the assignment operator for assigning values to different variables.

OperatorNameSyntaxExplanation
Assign (=)Assignment$a = $bAssigns the value of $b to $a.
Add then Assign (+=)Add and Assign$a += $b$a becomes the sum of $a and $b.
Subtract then Assign (-=)Subtract and Assign$a -= $b$a becomes the difference of $a and $b.
Multiply then Assign (*=)Multiply and Assign$a *= $b$a becomes the product of $a and $b.
Divide then Assign (/=)Divide and Assign$a /= $b$a becomes the quotient of $a and $b.
Modulus then Assign (%=)Modulus and Assign$a %= $b$a becomes the remainder of $a divided by $b.

Bitwise Operators

We use Bitwise operators to perform bit-level operations on operands.

OperatorNameSyntaxExplanation
AND (&)Bitwise AND$a & $bBits set to 1 in both $a and $b become 1.
**OR ()**Bitwise OR`$a
XOR (^)Bitwise XOR$a ^ $bBits set to 1 in either $a or $b but not both become 1.
NOT (~)Bitwise NOT~$aInverts the bits of $a.
Left Shift (<<)Shift Left$a << $bShifts bits of $a to the left by $b positions.
Right Shift (>>)Shift Right$a >> $bShifts bits of $a to the right by $b positions.

Read about Bitwise Operators in C here.

Comparison Operators

We use comparison operators for comparing two values such as number or string.

OperatorNameSyntaxExplanation
Equal (==)Equality$a == $bReturns TRUE if $a is equal to $b.
Identical (===)Identical$a === $bReturns TRUE if $a is equal to $b and both have the same data type.
Not Equal (!=)Inequality$a != $bReturns TRUE if $a is not equal to $b.
Not Equal (<>)Inequality$a <> $bReturns TRUE if $a is not equal to $b.
Not Identical (!==)Not Identical$a !== $bReturns TRUE if $a is not equal to $b or their data types are different.
Less Than (<)Less Than$a < $bReturns TRUE if $a is less than $b.
Greater Than (>)Greater Than$a > $bReturns TRUE if $a is greater than $b.
Less Than or Equal (<=)Less Than or Equal$a <= $bReturns TRUE if $a is less than or equal to $b.
Greater Than or Equal (>=)Greater Than or Equal$a >= $bReturns TRUE if $a is greater than or equal to $b.
Spaceship (<=>)Spaceship$a <=> $bReturns -1 if $a < $b, 0 if $a = $b, 1 if $a > $b.

Incrementing/Decrementing Operators

We use these operators for incrementing and decrementing the value of a variable.

OperatorNameSyntaxExplanation
Post Increment (++)Increment$a++Increments $a by 1, then returns $a.
Pre Increment (++)Increment++$aIncrements $a by 1, then returns $a.
Post Decrement (--)Decrement$a--Decrements $a by 1, then returns $a.
Pre Decrement (--)Decrement--$aDecrements $a by 1, then returns $a.

Logical Operators

We use logical operators to perform bit-level operations on operands.

OperatorNameSyntaxExplanation
AND (and, &&)Logical AND$a and $b, $a && $bReturns TRUE if both $a and $b are TRUE.
**OR (or, )**Logical OR
XOR (xor)Logical XOR$a xor $bReturns TRUE if $a or $b is TRUE, but not both.
NOT (!)Logical NOT!$aReturns TRUE if $a is not TRUE.

String Operators

We use string operators to perform operations on strings.

OperatorNameSyntaxExplanation
Concatenation (.)Concatenate$a . $bConcatenates $a and $b.
Concatenation and Assignment (.=)Concatenate and Assign$a .= $bConcatenates $b to $a and assigns it to $a.

Array Operators

We use array operators to compare the values of arrays.

OperatorNameSyntaxExplanation
Union (+)Union$a + $bCombines arrays $a and $b.
Equality (==)Equality$a == $bReturns TRUE if arrays $a and $b have same key-value pairs.
Inequality (!=)Inequality$a != $bReturns TRUE if arrays $a and $b are not equal.
Identity (===)Identity$a === $bReturns TRUE if arrays $a and $b have same key-value pairs in the same order.
Non-Identity (!==)Non-Identity$a !== $bReturns TRUE if arrays $a and $b are not identical.

Execution Operators

PHP uses backticks to run shell commands.

OperatorNameSyntaxExplanation
Backticks (``)Execute Shell Command`$a`Executes the shell command and returns the result.

Error Control Operators

When we use the error control operator with an expression, the error message gets ignored by that expression.

OperatorNameSyntaxExplanation
At (@)Error Control@expressionSuppresses error messages from an expression.

Conditional Assignment Operators

Conditional Assignment Operators are used to setting values based on the following situations.

OperatorNameSyntaxExplanation
Ternary (?:)Ternary$a = expr1 ? expr2 : expr3Returns expr2 if expr1 is TRUE; otherwise, returns expr3.
Null Coalescing (??)Null Coalescing$a = expr1 ?? expr2Returns expr1 if it is not NULL, otherwise returns expr2.

Frequently Asked Questions

What is '@' in PHP?

The @ operator in PHP suppresses error messages generated by an expression, preventing them from being displayed.

Why do we use :: in PHP?

The :: operator in PHP is used to access static methods, constants, or properties within a class.

What is the operator and type?

An operator in PHP is a symbol that performs operations on variables and values, such as arithmetic, logical, and comparison operations.

Conclusion

This article teaches about PHP Operators and how we use them. We saw why it could be beneficial for a developer to learn. Click here to read about PHP Interview Questions.

Also, check out our web development technologies and blogs on Backend Web Technologies.

Live masterclass