1

The following code:

$arr = array(1, 2, 3, 4, 5);
$i = 5;
while($i >= 1){
    var_dump($i);
    var_dump($arr[--$i]);
}

has the following output:

int(5)
int(5)
int(4)
int(4)
int(3)
int(3)
int(2)
int(2)
int(1)
int(1)

but if we replace the two lines within the while loop with the single line:

var_dump($i == $arr[--$i]);

the code has the following output:

bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

The first output shows that $i and $arr[--$i] are equal.

The second output shows that $i and $arr[--$i] are not equal.

Why this discrepancy? What am I missing?

Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
  • You can boil it down to just `var_dump($i == --$i)`, which perhaps surprisingly is `true`. – deceze Jun 06 '23 at 06:53
  • 1
    @Álvaro I think OP is very aware of the index 4 = 5 thing and that's the expected outcome. The question is why the behaviour differs when combining the two expressions into one. – deceze Jun 06 '23 at 11:13

0 Answers0