What is the difference between $var++ and $var + 1 in PHP?

$var + 1 only calculates a new value and does not change $var. $var++ is the increment operator, it changes the value of $var by adding 1.

There is one more thing to remember. $var++ (post-increment) returns the old value first and then increases. ++$var (pre-increment) increases first and then returns the new value.

<?php
$a = 5;
$b = $a + 1;      // $a is not changed
echo $a . " " . $b . "<br>";

$x = 5;
$y = $x++;        // $y gets old value 5, then $x becomes 6
echo $x . " " . $y . "<br>";

$m = 5;
$n = ++$m;        // $m becomes 6 first, then $n gets 6
echo $m . " " . $n;
?>

Output:

Tutorials Class - Output Window

5 6
6 5
6 6


Learn more about the similar topics:
Tutorials
PHP Operators
PHP Ternary Operator
Exercises & Assignments
Write a PHP program to add two numbers
Interview Questions & Answers
What is the difference between == and === Operators in PHP?
What are the arithmetic operators in PHP?
What is the use of the ternary operator in PHP?
What is the null coalescing operator (??) in PHP?
What is the difference between division (/) and modulus (%) in PHP?
What is the difference between != and !== in PHP?
What is the difference between $var++ and $var + 1 in PHP?