Operator Types in PHP

PHP Operators Complete Guide | Arithmetic, Assignment, Comparison & Logical Operators in PHP

PHP Operators – Complete Beginner to Advanced Guide

Welcome to phponline.in, your trusted learning hub for PHP.
This complete PHP Operators guide covers everything — from basic arithmetic to advanced bitwise operations, with syntax, examples, real-world use cases, and performance tips.


What you learn

  1. Introduction to PHP Operators
  2. What is an Operator in PHP?
  3. PHP Arithmetic Operators
  4. PHP Assignment Operators
  5. PHP Comparison Operators
  6. PHP Logical Operators
  7. PHP Increment & Decrement Operators
  8. PHP String Operators
  9. PHP Array Operators
  10. PHP Type Operators (instanceof)
  11. PHP Bitwise Operators
  12. PHP Ternary and Null Coalescing Operators
  13. Operator Precedence & Associativity
  14. Combining Operators in PHP
  15. Real-World Examples of Operators
  16. Common Mistakes with Operators
  17. Best Practices for Using Operators
  18. Performance Considerations for Operators

1. Introduction to PHP Operators

In PHP, operators are symbols or keywords that perform operations on variables and values.
Example:

$x = 10 + 5; // + is an arithmetic operator

2. What is an Operator in PHP?

An operator tells PHP to perform a specific action such as addition, comparison, assignment, or logical evaluation.


3. PHP Arithmetic Operators

OperatorDescriptionExampleResult
+Addition$x + $ySum of $x and $y
-Subtraction$x - $yDifference
*Multiplication$x * $yProduct
/Division$x / $yQuotient
%Modulus$x % $yRemainder
**Exponentiation$x ** $yPower

Example:

$a = 10;  
$b = 3;  
echo $a % $b; // Output: 1

4. PHP Assignment Operators

OperatorExampleEquivalent To
=$x = 10$x = 10
+=$x += 5$x = $x + 5
-=$x -= 5$x = $x - 5
*=$x *= 5$x = $x * 5
/=$x /= 5$x = $x / 5
%=$x %= 5$x = $x % 5

5. PHP Comparison Operators

OperatorDescriptionExample
==Equal$x == $y
===Identical (value & type)$x === $y
!= or <>Not equal$x != $y
!==Not identical$x !== $y
>Greater than$x > $y
<Less than$x < $y
>=Greater or equal$x >= $y
<=Less or equal$x <= $y
<=>Spaceship$x <=> $y

Example:

echo 5 <=> 10; // Output: -1

6. PHP Logical Operators

OperatorDescriptionExample
&&AND$x && $y
``
!NOT!$x
andAND (low precedence)$x and $y
orOR (low precedence)$x or $y
xorXOR$x xor $y

7. PHP Increment & Decrement Operators

OperatorExampleDescription
++$xPre-incrementIncrement before use
$x++Post-incrementIncrement after use
--$xPre-decrementDecrement before use
$x--Post-decrementDecrement after use

8. PHP String Operators

OperatorDescriptionExample
.Concatenation$a . $b
.=Concatenation assignment$a .= $b

9. PHP Array Operators

OperatorDescriptionExample
+Union$a + $b
==Equality$a == $b
===Identity$a === $b
!= or <>Inequality$a != $b
!==Non-identity$a !== $b

10. PHP Type Operators (instanceof)

Example:

class Test {}
$obj = new Test();
var_dump($obj instanceof Test); // true

11. PHP Bitwise Operators

OperatorDescription
&AND
``
^XOR
~NOT
<<Shift left
>>Shift right

12. PHP Ternary & Null Coalescing Operators

Ternary:

$result = ($age >= 18) ? "Adult" : "Minor";

Null Coalescing:

$name = $_GET['name'] ?? 'Guest';

13. Operator Precedence & Associativity

PHP follows precedence rules. Example:

echo 5 + 3 * 2; // 11

14. Combining Operators in PHP

You can chain multiple operators:

$total = ($a + $b) * $c / $d;

15. Real-World Examples

  • Calculating discount prices
  • Checking login credentials
  • Validating form inputs
  • Working with binary permissions

16. Common Mistakes

  • Confusing = with ==
  • Using and instead of && in complex expressions
  • Not understanding precedence

17. Best Practices

  • Use strict comparison (===)
  • Use parentheses for clarity
  • Avoid long chained operations

18. Performance Considerations

  • Logical operators short-circuit evaluation
  • Bitwise operations are faster for flags

Learn more


Frequently Asked Questions (FAQs)

Q1: What is the difference between == and === in PHP?
A: == compares values, === compares values and types.

Q2: Which operator is used for string concatenation?
A: The . (dot) operator.

Q3: What is the spaceship operator?
A: <=> returns -1, 0, or 1 for comparisons.

Q4: What is ?? in PHP?
A: Null coalescing operator, returns first non-null value.

Q5: Are bitwise operators used often?
A: Mostly in low-level operations and permission flags.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments