PHP Operators

Introduction

In PHP, operators are used to perform operations on variables and values. Whether you are doing mathematical calculations, comparing values, or working with conditions, operators play a key role.

In this article, we will explore different types of PHP operators, their syntax, examples, and real-world usage.

What is a PHP Operator?

A PHP Operator is a symbol used to perform operations on one or more operands (values or variables).

For example:


$x = 10 + 5;

Here:

  • + is the operator
  • 10 and 5 are operands

Why it is Used

1. Perform Calculations

Used for arithmetic operations like addition, subtraction, etc.

2. Decision Making

Used in conditions (if, else) to compare values.

3. Data Manipulation

Helps modify or combine data.

4. Logical Operations

Used for checking multiple conditions.

Types of PHP Operators

PHP operators are mainly divided into two categories:

1. Unary Operators

2. Binary Operators

1. Unary Operators.

Unary operators work on a single operand.

a) Increment Operator

(i) Post-Increment

First returns the value, then increases by 1.


$x = 100;
echo $x++;

Output:

100

(ii) Pre-Increment

First increases the value, then returns.


$x = 100;
echo ++$x;

Output

101

b) Decrement Operator

Post-Decrement

First returns the value, then decreases by 1.


$x = 100;
echo $x--;

Output:

100

Pre-Decrement

First decrease the value, then return.


$x = 100;
echo --$x;

Output:

99

c) Cast Operators

Cast operators are used to convert data types.

1. Convert to array data type


$x = 100;
$y = (array)$x;
print_r($y);

Output:

Array ( [0] => 100 )

2. Convert to int data type


<?php
$x = "100";
$y = (int)$x;
echo $y;
?>

Output:

100

3. Convert to string data type


<?php
$x = 100;
$y = (string)$x;
echo $y;
?>

Output:

100

4. Convert to object data type


<?php
$x = 100;
$y = (object)$x;
print_r($y);
?>

Output:

stdClass Object ( [scalar] => 100 )

5. Convert to float / double data type


<?php
$x = "100";
$y = (double)$x;
echo gettype($y);
?>

Output:

double

6. Convert to bool data type


<?php
$x = "100";
$y = (bool)$x;
echo gettype($y);
?>

Output:

boolean

d) Negation Operators

a) Logical Negation (!)


<?php
$x = 100;

if(!$x){
   echo 'x variable does not exist';
}else{
   echo 'x variable exists';
}
?>

Output:

x variable exists

b) Bitwise Negation (~)

This operator flips bits (0 → 1, 1 → 0).

2. Binary Operators

Binary operators work on two operands.

1. Numeric (Arithmetic) Operators

Addition (+)


$x = 100;
$y = 40;
echo $x + $y;

Subtraction (-)


echo $x - $y;

Multiplication (*)


echo $x * $y;

Division (/)


echo $x / $y;

Modulus (%)


echo $x % $y;

2. Assignment Operators

Used to assign values to variables.


$x = 2;
$x += 3; // x = x + 3
$x -= 1; // x = x - 1
$x *= 2; // x = x * 2
$x /= 2; // x = x / 2
$x %= 2; // x = x % 2

3. Comparison Operators

Used to compare two values.

  • == → Equal
  • != → Not equal
  • > → Greater than
  • < → Less than
  • >= → Greater than or equal
  • <= → Less than or equal

Example:


<?php
$x = 100;
$y = 40;

var_dump($x > $y);
?>

4. Logical Operators

AND (&&)

True if both conditions are true.


$x && $y

OR (||)

True if at least one condition is true.


$x || $y

XOR

True if only one condition is true.


$x xor $y

5. Concatenation Operator

Used to join strings.


<?php
$emp_age = 23;
echo "Employee age is " . $emp_age;
?>

Output:

Employee age is 23

6. Conditional Assignment Operators

1. Ternary Operator (?:)


<?php
$x = 10;
echo ($x == 10) ? 'even number' : 'odd number';
?>

Output:

even number

2. Null Coalescing Operator (??)


<?php
$employee = 'John';
$employee_name = $employee ?? 'employee not exists';
?>

Equivalent to:


isset($employee) ? $employee : 'employee not exists';

Real-Life Example

Example: Login Check


<?php
$isLoggedIn = true;
echo ($isLoggedIn) ? "Welcome User" : "Please Login";
?>

Note: Operators help control application flow.

Common Mistakes

1. Confusing = and ==

  •  → Assignment
  • == → Comparison

2. Using the Wrong Operator

Using + instead of . for strings.

3. Ignoring Operator Precedence

Wrong calculation results due to order of operations.

4. Not Using Parentheses

Can lead to unexpected outputs.

Conclusion

In PHP, understanding different types of operators such as unary, binary, arithmetic, logical, and conditional operators will help you write efficient and clean code. Avoid common mistakes and practice using operators in real-world scenarios to strengthen your skills.

PHP Operator – Interview Questions

Q 1: What are operators in PHP?
Ans: Operators are symbols used to perform operations on variables and values.
Q 2: Name the main types of PHP operators.
Ans: Arithmetic, Assignment, Comparison, Logical, Increment/Decrement.
Q 3: What is the difference between == and ===?
Ans: == compares values, === compares both value and data type.
Q 4: What is the logical AND operator in PHP?
Ans: && or and.
Q 5: What is the use of the concatenation operator?
Ans: The . operator joins two strings.

PHP Operator – Objective Questions (MCQs)

Q1. Which operator is used for equality comparison in PHP?






Q2. What does === operator check?






Q3. Which operator is used for concatenating strings?






Q4. Which operator represents logical AND?






Q5. The modulus operator % returns ______.






Related PHP Tutorials