Increment and decrement operators in PHP are used to increase or decrease a variable’s value by one. These operators are useful when counting students, tracking product quantities, updating scores, and repeating tasks in loops. In this chapter, you will practice 10 PHP questions covering pre-increment, post-increment, pre-decrement, and post-decrement with solutions and sample outputs. PHP Increment and Decrement Operators Practice Questions with Solutions to help you understand the concepts.
Question 1: Increase a Number Using the Increment Operator (++)
Problem Statement: Write a PHP program to increase a number from 10 to 11 using the increment operator.
PHP Solution:
<?php
$number = 10;
$number++;
echo "Updated Number: " . $number;
?>
Sample Output:
Updated Number: 11
Explanation: The ++ operator increases the variable’s value by one. The statement $number++; is equivalent to $number = $number + 1;.
Concepts Covered: Increment operator, variables, and arithmetic operations.
Question 2: Decrease a Number Using the Decrement Operator (--)
Problem Statement: A store has 50 products in stock. One product is sold. Write a PHP program to decrease the stock quantity by one.
PHP Solution:
<?php
$stock = 50;
$stock--;
echo "Remaining Stock: " . $stock;
?>
Sample Output:
Remaining Stock: 49
Explanation: The -- operator decreases a variable’s value by one. It is useful for updating stock quantities, counters, and other values that decrease over time.
Concepts Covered: Decrement operator, stock management, and variables.
Question 3: Use Pre-Increment (++$number)
Problem Statement: Write a PHP program to demonstrate pre-increment by increasing a variable before displaying its value.
PHP Solution:
<?php
$number = 5;
echo ++$number;
?>
Sample Output:
6
Explanation: Pre-increment increases the variable’s value first and then uses the updated value.
In ++$number, the variable changes from 5 to 6 before echo displays it.
Concepts Covered: Pre-increment, expression evaluation, and output.
Question 4: Use Post-Increment ($number++)
Problem Statement: Write a PHP program to demonstrate post-increment by displaying a variable’s current value before increasing it.
PHP Solution:
<?php
$number = 5;
echo $number++;
echo "<br>";
echo $number;
?>
Sample Output:
5
6
Explanation: Post-increment uses the variable’s original value in the current expression and then increases it by one.
- The first
echodisplays5. - The variable then increases to
6. - The second
echodisplays6.
Concepts Covered: Post-increment, expression evaluation, and variable updates.
Question 5: Use Pre-Decrement (--$number)
Problem Statement: A countdown starts at 10. Write a PHP program to decrease the value before displaying it.
PHP Solution:
<?php
$countdown = 10;
echo --$countdown;
?>
Sample Output:
9
Explanation: Pre-decrement decreases the variable’s value first and then uses the updated value. The countdown changes from 10 to 9 before the output is displayed.
Concepts Covered: Pre-decrement, countdowns, and expressions.
Question 6: Use Post-Decrement ($number--)
Problem Statement: Write a PHP program to display a countdown variable’s current value before decreasing it.
PHP Solution:
<?php
$countdown = 10;
echo $countdown--;
echo "<br>";
echo $countdown;
?>
Sample Output:
10
9
Explanation: Post-decrement uses the original value in the current expression and decreases the variable afterward.
The first output is 10. The variable then becomes 9, which is displayed by the second echo.
Concepts Covered: Post-decrement, expression evaluation, and variable updates.
Question 7: Compare Pre-Increment and Post-Increment
Problem Statement: Write a PHP program to compare the results of pre-increment and post-increment using two variables.
PHP Solution:
<?php
$a = 10;
$b = 10;
echo "Pre-Increment: " . (++$a) . "<br>";
echo "Post-Increment: " . ($b++) . "<br>";
echo "Value of b after increment: " . $b;
?>
Sample Output:
Pre-Increment: 11
Post-Increment: 10
Value of b after increment: 11
Explanation: Pre-increment updates the variable before its value is used in the expression. Post-increment uses the original value first and updates the variable afterward.
Therefore, pre-increment displays 11, while post-increment initially displays 10.
Concepts Covered: Pre-increment, post-increment, and comparison of expressions.
Question 8: Update Student Attendance Using Increment
Problem Statement: A teacher records attendance for students one at a time. Write a PHP program to count three students using the increment operator.
PHP Solution:
<?php
$students = 0;
$students++;
$students++;
$students++;
echo "Students Present: " . $students;
?>
Sample Output:
Students Present: 3
Explanation: The variable $students starts at 0. Each increment increases the count by one. After three increments, the total becomes 3.
Concepts Covered: Increment operator, counters, and attendance tracking.
Question 9: Calculate Remaining Tickets Using Decrement
Problem Statement: An event has 100 tickets available. Three tickets are sold individually. Write a PHP program to calculate the remaining tickets using the decrement operator.
PHP Solution:
<?php
$tickets = 100;
$tickets--;
$tickets--;
$tickets--;
echo "Remaining Tickets: " . $tickets;
?>
Sample Output:
Remaining Tickets: 97
Explanation: The ticket count decreases by one each time a ticket is sold. After three decrements, the remaining quantity is 97.
Concepts Covered: Decrement operator, ticket management, and counters.
Question 10: Predict the Output of Increment and Decrement Operations
Problem Statement: Write a PHP program that uses pre-increment, post-increment, pre-decrement, and post-decrement. Display each result and the final variable values.
PHP Solution:
<?php
$a = 5;
$b = 5;
$c = 8;
$d = 8;
echo "Pre-Increment: " . (++$a) . "<br>";
echo "Post-Increment: " . ($b++) . "<br>";
echo "Pre-Decrement: " . (--$c) . "<br>";
echo "Post-Decrement: " . ($d--) . "<br>";
echo "Final a: " . $a . "<br>";
echo "Final b: " . $b . "<br>";
echo "Final c: " . $c . "<br>";
echo "Final d: " . $d;
?>
Sample Output:
Pre-Increment: 6
Post-Increment: 5
Pre-Decrement: 7
Post-Decrement: 8
Final a: 6
Final b: 6
Final c: 7
Final d: 7
Explanation: This program demonstrates all four operators:
++$aincreases$abefore using its value.$b++uses the original value before increasing$b.--$cdecreases$cbefore using its value.$d--uses the original value before decreasing$d.
The final values show that all four variables have been updated by one in the appropriate direction.
Concepts Covered: Pre-increment, post-increment, pre-decrement, post-decrement, and expression evaluation.
Key Takeaways
++$xis the pre-increment operator. It increases the value before using it in the expression.$x++is the post-increment operator. It uses the original value in the expression before increasing it.--$xis the pre-decrement operator. It decreases the value before using it in the expression.$x--is the post-decrement operator. It uses the original value in the expression before decreasing it.- Increment and decrement operators change a variable’s value by one.
- These operators are useful for counters, attendance systems, stock tracking, and countdowns.
- The difference between pre- and post-operators is especially important when they appear inside expressions.
Frequently Asked Questions
1. What are increment and decrement operators in PHP?
Increment and decrement operators increase or decrease a variable’s value by one.
2. What is the difference between ++$x and $x++?
++$x increases the variable before using its value in the expression. $x++ uses its original value in the expression before increasing it.
3. What does the -- operator do in PHP?
The -- operator decreases a variable’s value by one.
4. Can increment and decrement operators be used with integers?
Yes. They are commonly used with integer variables for counting and arithmetic operations.
5. What is pre-decrement in PHP?
Pre-decrement, written as --$x, decreases the variable first and then uses the updated value in the expression.
6. What is post-decrement in PHP?
Post-decrement, written as $x--, uses the variable’s original value in the expression and decreases it afterward.
7. Are increment and decrement operators useful in loops?
Yes. They are often used in for loops to increase or decrease counters during repeated operations.
8. Does $x++ immediately change the variable?
Yes. The variable is incremented as part of evaluating the expression, although the expression itself uses the original value.
9. Can increment and decrement operators be used with strings in PHP?
PHP’s behavior with strings depends on the string’s contents and PHP version. For beginner practice, use numeric variables to understand these operators clearly.
10. What is the main benefit of increment and decrement operators?
They provide a short, readable way to increase or decrease a variable by one without writing a longer assignment expression.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
