Operators
Learn PHP operators — arithmetic, comparison, logical, assignment, and more — with real shopping cart examples and professional interview answers.
Welcome to PHP Operators
Imagine you're building an online shopping application.
When a customer places an order, your application needs to:
- Calculate the total price.
- Apply discounts.
- Compare payment amounts.
- Check whether a user is logged in.
- Verify stock availability.
How does PHP perform all these operations?
The answer is Operators.
Operators are symbols that tell PHP to perform a specific task, such as adding numbers, comparing values, or making logical decisions.
Without operators, your application would not be able to calculate prices, validate login credentials, or process business rules.
What are Operators?
An Operator is a special symbol that performs an operation on one or more values.
Think of operators as instructions.
For example:
- Add two numbers
- Compare two values
- Assign a value
- Check multiple conditions
- Join two strings
Operators are used in almost every PHP program.
Why Do We Need Operators?
Imagine a food delivery application.
The application needs to answer questions like:
- Is the customer eligible for free delivery?
- Is the order amount greater than ₹500?
- Is the payment successful?
- Should GST be added?
- How much is the final bill?
These calculations and decisions are made using operators.
Types of Operators in PHP
PHP provides several categories of operators.
| Operator Type | Purpose |
|---|---|
| Arithmetic | Perform mathematical calculations |
| Assignment | Assign values to variables |
| Comparison | Compare two values |
| Logical | Combine multiple conditions |
| Increment & Decrement | Increase or decrease values |
| String | Join text together |
| Array | Compare arrays |
| Bitwise | Perform bit-level operations |
| Null Coalescing | Handle null values safely |
We'll learn each category in dedicated lessons.
Today, you'll understand the overall idea behind operators.
Arithmetic Operators Example
Arithmetic operators perform mathematical calculations.
<?php$price = 1000;$gst = 180;$total = $price + $gst;echo $total;?>
Output
1180
Here, the + operator adds two values.
Comparison Operator Example
Comparison operators help PHP compare values.
<?php$age = 20;var_dump($age >= 18);?>
Output
bool(true)
PHP checks whether the value is greater than or equal to 18.
Logical Operator Example
Suppose a banking application allows loans only if:
- Customer age is above 21.
- Monthly salary is above ₹50,000.
<?php$age = 28;$salary = 70000;if ($age >= 21 && $salary >= 50000) {echo "Loan Approved";}?>
The && operator ensures both conditions are true.
Real-World Example
Imagine you're building an online shopping cart.
<?php$productPrice = 2500;$quantity = 2;$discount = 500;$total = ($productPrice * $quantity) - $discount;echo "Final Amount: ₹" . $total;?>
Output
Final Amount: ₹4500
Here, multiple operators work together to calculate the final bill.
Best Practices
- Use parentheses to make calculations easier to read.
- Keep expressions simple and readable.
- Use meaningful variable names.
- Avoid combining too many operators in one statement.
- Test complex conditions before deploying your application.
Common Mistakes
Mixing Assignment (=) and Comparison (==)
Incorrect
if ($age = 18)
Correct
if ($age == 18)
Writing Complex Conditions
Avoid long, unreadable expressions. Break complex logic into smaller variables.
Ignoring Operator Precedence
Use parentheses whenever calculations become difficult to understand.
Hands-on Exercise
Task: Create variables for Product Price, Quantity, and Discount. Calculate the final amount.
Challenge: Create an employee bonus calculator with Salary and Bonus Percentage. Calculate the total salary after adding the bonus.
<?php// TODO: Task$productPrice = 2500;$quantity = 2;$discount = 500;// $total = ...// TODO: Challenge — bonus calculator?>
Summary
- Operators perform calculations, comparisons, and logical decisions.
- PHP provides different operator categories for different tasks.
- Operators are essential for building dynamic applications.
- Arithmetic operators calculate values.
- Comparison and logical operators help applications make decisions.
Key Takeaways
- Operators make PHP programs interactive and dynamic.
- Different operators solve different problems.
- Keep expressions simple and readable.
- Always understand the difference between assignment and comparison.
- Operator knowledge is fundamental to every PHP application.
Interview Questions & Professional Answers
Q1What is an operator in PHP?
Professional Answer
An operator is a symbol that instructs PHP to perform an operation on one or more operands. Operators are used for mathematical calculations, assigning values, comparing data, making logical decisions, and manipulating strings or arrays. Almost every PHP application uses operators to process user input and implement business logic.
Example
$total = 100 + 50;
Follow-up Question: Where are operators commonly used?
Operators are used in calculations, conditions, loops, authentication, validation, database queries, and business rules.
Interview Tip: Mention real examples like price calculations, tax computation, login validation, or inventory checks.
Q2What are the different types of operators in PHP?
Professional Answer
PHP provides several categories of operators, including Arithmetic, Assignment, Comparison, Logical, Increment/Decrement, String, Array, Bitwise, and Null Coalescing operators. Each category is designed for a specific purpose, such as calculations, comparisons, or decision-making.
Follow-up Question: Which operators are used most frequently in web applications?
Arithmetic, Assignment, Comparison, and Logical operators are used extensively in almost every PHP application.
Q3What is the difference between = and ==?
Professional Answer
The = operator assigns a value to a variable, whereas the == operator compares two values for equality. Using = instead of == inside conditions is a common beginner mistake and can lead to unexpected results.
Example
$age = 18; // Assignment$age == 18; // Comparison
Interview Tip: Also mention === (strict comparison) if you're familiar with it. It compares both value and data type.
Q4Why are operators important in PHP?
Professional Answer
Operators allow applications to perform calculations, evaluate conditions, process user input, and implement business rules. Without operators, applications would not be able to calculate totals, validate login credentials, compare values, or make decisions.
Follow-up Question: Give a real-world example.
An e-commerce website uses operators to calculate the final order amount after applying discounts and taxes.
Q5What is operator precedence?
Professional Answer
Operator precedence determines the order in which PHP evaluates operators in an expression. For example, multiplication is evaluated before addition.
Example
$result = 10 + 5 * 2; // Result: 20$result = (10 + 5) * 2; // Result: 30
Interview Tip: In production code, use parentheses even when you know the precedence rules. It makes the code easier to read and maintain.
What's Next?
Now that you understand what operators are and why they are important, it's time to explore them one category at a time.
In the next lesson, Type Casting, you'll learn how PHP converts values between data types — a skill you'll use constantly alongside operators in real applications.