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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.