Introduction
PHP operators are symbols used to perform calculations, compare values, assign data, and combine conditions. An expression is a combination of values, variables, and operators that produces a result. For example, $total = $price + $tax uses the addition operator inside an expression. In this chapter, you will practice PHP operators and expressions through examples using marks, salaries, shopping bills, and other practical situations. PHP Operators and Expressions Practice Questions with Solutions to help you understand the concepts.
Question 1: Calculate the Total and Average Marks
Problem Statement: Create variables for marks in three subjects. Use arithmetic operators to calculate the total and average marks.
PHP Solution:
<?php
$english = 80;
$maths = 90;
$computer = 85;
$total = $english + $maths + $computer;
$average = $total / 3;
echo "Total Marks: " . $total . "<br>";
echo "Average Marks: " . $average;
?>
Sample Output:
Total Marks: 255
Average Marks: 85
Explanation: The + operator adds the marks, while / divides the total by the number of subjects. The expressions $english + $maths + $computer and $total / 3 produce the total and average.
Concepts Covered: Arithmetic operators, variables, expressions.
Question 2: Calculate a Shopping Bill Using Operators
Problem Statement: A customer buys three notebooks at ₹50 each and two pens at ₹20 each. Calculate the subtotal, apply a ₹30 discount, and display the final bill.
PHP Solution:
<?php
$notebookPrice = 50;
$notebookQuantity = 3;
$penPrice = 20;
$penQuantity = 2;
$subtotal = ($notebookPrice * $notebookQuantity)
+ ($penPrice * $penQuantity);
$discount = 30;
$finalBill = $subtotal - $discount;
echo "Subtotal: Rs. " . $subtotal . "<br>";
echo "Discount: Rs. " . $discount . "<br>";
echo "Final Bill: Rs. " . $finalBill;
?>
Sample Output:
Subtotal: Rs. 190
Discount: Rs. 30
Final Bill: Rs. 160
Explanation: The * operator calculates each product’s cost. The + operator adds both costs, and the - operator subtracts the discount.
Concepts Covered: Multiplication, addition, subtraction, arithmetic expressions.
Question 3: Compare Two Employees’ Salaries
Problem Statement: Store the salaries of two employees. Use comparison operators to check whether their salaries are equal, whether the first employee earns more, and whether the second employee earns less.
PHP Solution:
<?php
$salary1 = 45000;
$salary2 = 40000;
var_dump($salary1 == $salary2);
var_dump($salary1 > $salary2);
var_dump($salary2 < $salary1);
?>
Sample Output:
bool(false)
bool(true)
bool(true)
Explanation: The == operator checks equality, > checks whether one value is greater than another, and < checks whether one value is smaller. Each comparison produces a Boolean result: true or false.
Concepts Covered: Comparison operators, Boolean expressions.
Question 4: Use Logical Operators to Check Eligibility
Problem Statement: A student is eligible for a certificate if their marks are at least 60 and their attendance is at least 75%. Use a logical operator to check eligibility.
PHP Solution:
<?php
$marks = 72;
$attendance = 80;
$eligible = ($marks >= 60) && ($attendance >= 75);
echo "Eligible for Certificate: ";
var_dump($eligible);
?>
Sample Output:
Eligible for Certificate: bool(true)
Explanation: The && operator means logical AND. Both conditions must be true for $eligible to become true. If either condition is false, the result is false.
Concepts Covered: Logical AND, comparison expressions, Boolean values.
Question 5: Update a Product Price Using Assignment Operators
Problem Statement: A product costs ₹1,000. Increase its price by ₹200, apply a discount of ₹100, and then double the resulting amount using compound assignment operators.
PHP Solution:
<?php
$price = 1000;
$price += 200;
echo "After Increase: Rs. " . $price . "<br>";
$price -= 100;
echo "After Discount: Rs. " . $price . "<br>";
$price *= 2;
echo "After Multiplication: Rs. " . $price;
?>
Sample Output:
After Increase: Rs. 1200
After Discount: Rs. 1100
After Multiplication: Rs. 2200
Explanation: The += operator adds a value to the existing variable, -= subtracts a value, and *= multiplies the variable by a value. These operators update the original variable.
Concepts Covered: Assignment operators, compound assignments.
Question 6: Calculate a Remainder Using the Modulus Operator
Problem Statement: A teacher has 23 students and wants to form groups of 4. Calculate how many complete groups can be made and how many students remain.
PHP Solution:
<?php
$students = 23;
$groupSize = 4;
$completeGroups = intdiv($students, $groupSize);
$remainingStudents = $students % $groupSize;
echo "Complete Groups: " . $completeGroups . "<br>";
echo "Remaining Students: " . $remainingStudents;
?>
Sample Output:
Complete Groups: 5
Remaining Students: 3
Explanation: intdiv() returns the integer quotient of the division. The modulus operator % returns the remainder. Therefore, 23 students can form five complete groups of four, with three students remaining.
Concepts Covered: Modulus operator, integer division, arithmetic expressions.
Question 7: Understand Increment and Decrement PHP Operators
Problem Statement: Start with a variable containing 10. Increase it by one, then decrease it by one. Display the value after each operation.
PHP Solution:
<?php
$count = 10;
$count++;
echo "After Increment: " . $count . "<br>";
$count--;
echo "After Decrement: " . $count;
?>
Sample Output:
After Increment: 11
After Decrement: 10
Explanation: The increment operator ++ increases a variable by one. The decrement operator -- decreases it by one. These are commonly used in loops and counters.
Concepts Covered: Increment, decrement, variable updates.
Question 8: Use the Ternary Operator to Check Pass or Fail
Problem Statement: A student passes an examination if their marks are 40 or higher. Use the ternary operator to display Pass or Fail.
PHP Solution:
<?php
$marks = 55;
$result = ($marks >= 40) ? "Pass" : "Fail";
echo "Marks: " . $marks . "<br>";
echo "Result: " . $result;
?>
Sample Output:
Marks: 55
Result: Pass
Explanation: The ternary operator ? : is a short form of a simple conditional expression. If $marks >= 40 is true, the expression returns "Pass"; otherwise, it returns "Fail".
Concepts Covered: Ternary operator, comparison expressions, conditional results.
Question 9: Understand Operator Precedence
Problem Statement: Calculate the result of 10 + 5 * 2 and compare it with (10 + 5) * 2.
PHP Solution:
<?php
$result1 = 10 + 5 * 2;
$result2 = (10 + 5) * 2;
echo "Result Without Parentheses: " . $result1 . "<br>";
echo "Result With Parentheses: " . $result2;
?>
Sample Output:
Result Without Parentheses: 20
Result With Parentheses: 30
Explanation: Multiplication has higher precedence than addition, so PHP evaluates 5 * 2 first. Parentheses change the evaluation order, making PHP calculate 10 + 5 before multiplying by 2.
Concepts Covered: Operator precedence, parentheses, expression evaluation.
Question 10: Combine PHP Operators in an Employee Bonus Calculation
Problem Statement: An employee earns ₹40,000. They receive a 10% bonus if their salary is at least ₹35,000 and their performance rating is at least 4. Calculate the bonus and final salary using arithmetic, comparison, and logical operators.
PHP Solution:
<?php
$salary = 40000;
$rating = 5;
$isEligible = ($salary >= 35000) && ($rating >= 4);
$bonus = $isEligible ? $salary * 0.10 : 0;
$finalSalary = $salary + $bonus;
echo "Basic Salary: Rs. " . $salary . "<br>";
echo "Performance Rating: " . $rating . "<br>";
echo "Eligible for Bonus: ";
var_dump($isEligible);
echo "Bonus: Rs. " . $bonus . "<br>";
echo "Final Salary: Rs. " . $finalSalary;
?>
Sample Output:
Basic Salary: Rs. 40000
Performance Rating: 5
Eligible for Bonus: bool(true)
Bonus: Rs. 4000
Final Salary: Rs. 44000
Explanation: The >= operator checks the salary and rating requirements. The && operator combines both conditions. The ternary operator determines the bonus, while * and + calculate the bonus amount and final salary.
Concepts Covered: Arithmetic operators, comparison operators, logical operators, ternary expressions.
Key Takeaways
- PHP Operators perform calculations, comparisons, assignments, and logical checks.
- Expressions combine variables, values, and operators to produce results.
- Arithmetic operators include
+,-,*,/, and%. - Comparison operators return Boolean results.
- Logical operators such as
&&,||, and!combine or reverse conditions. - Assignment operators such as
+=and-=update variables. - Increment (
++) and decrement (--) change a value by one. - The ternary operator provides a short way to choose between two results.
- PHP Operator precedence determines the order in which expressions are evaluated.
- Parentheses help control evaluation order and make expressions easier to understand.
Frequently Asked Questions
1. What are operators in PHP(PHP operators)?
Operators are symbols that perform operations on values and variables, such as addition, comparison, assignment, and logical evaluation.
2. What is an expression in PHP operators?
An expression is a combination of values, variables, function calls, and operators that produces a value. For example, $price * 2 is an expression.
3. What are the main types of PHP operators?
Common categories include arithmetic, assignment, comparison, logical, increment/decrement, string, array, conditional, and bitwise operators.
4. What is the difference between = and ==?
The = operator assigns a value to a variable. The == operator compares two values for equality, allowing applicable type conversions.
5. What is the difference between == and ===?
== compares values with applicable type conversion, while === checks whether both the values and their data types match.
6. What does the modulus operator % do?
The % operator returns the remainder after division. For example, 10 % 3 returns 1.
7. What is the difference between && and ||?
The && operator returns true when both conditions are true. The || operator returns true when at least one condition is true.
8. What is the ternary operator in PHP?
The ternary operator ? : is a compact conditional expression that selects one of two values based on a condition.
9. What is operator precedence?
Operator precedence determines which operations PHP evaluates first. For example, multiplication is evaluated before addition unless parentheses change the order.
10. Why are operators important in PHP programming?
Operators are essential for calculations, validating conditions, comparing data, updating variables, and building practical programs such as salary calculators and billing systems.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
