+ 3

# Increment Question

<?php $x = 2; $y = $x++; echo $y; ?> Why the result is 2. x = 2, and when x ++ that Means should be 2 + 1. And the result is 3. But why 2?

6 Answers

+ 2

$y = $x++;
is equal to
$y = $x;
$x = $x + 1;
$y = ++$x;
is equal to
$x = $x + 1;
$y = $x;

+ 2

there is a lot of diff between x++ and ++x .
x++ means post incrementation, hence value of x++ remains same as that of x,and x is incremented later on.

+ 1

$y = $x++;
set current value of $x (2) to $y then increment $x to 3
obtain 3 with $y = ++$x;

+ 1

yes

+ 1

$y = $x++ means that you assign the value of $x (2) to $y and THEN you increase the $x value from 2 to 3. That's why echo $y is 2, not 3.

0

so, if i answer 3, this is value of $x?

Hot today

transition help

1 Votes

Как сравнивать строки в Phyton?

0 Votes

Python Question

0 Votes

Kotlin get if Var doesn't exist

0 Votes

Sololearn bug @mentors

2 Votes