My Question is why does the post increment not function on the first element but then works as expected when it is contained within an array [ ] element ?
Here is a snippet of what I was doing that behaves unexpectedly:
When I use this code …
$lessonIndex = 0;
$EventArray[$lessonIndex]['Title'] = 'Lesson 1';
$EventArray[$lessonIndex]['Start'] = $TimeArray[$EventArray[$lessonIndex++]['Title']]['Start'];
$EventArray[$lessonIndex]['Title'] = 'Lesson 2';
$EventArray[$lessonIndex]['Start'] = $TimeArray[$EventArray[$lessonIndex++]['Title']]['Start'];
$EventArray[$lessonIndex]['Title'] = 'Lesson 3';
$EventArray[$lessonIndex]['Start'] = $TimeArray[$EventArray[$lessonIndex++]['Title']]['Start'];
The $EventArray[$lessonIndex] contents will be skip index 0 then index 1 will get what was destined for index 0 and the increment proceeds as expected through the rest of the code, but with the results being off by one.
If I do this instead:
$lessonIndex = 0;
$EventArray[$lessonIndex]['Title'] = 'Lesson 1';
$EventArray[$lessonIndex]['Start'] = $TimeArray[$EventArray[$lessonIndex]['Title']]['Start'];
$lessonIndex++;
$EventArray[$lessonIndex]['Title'] = 'Lesson 2';
$EventArray[$lessonIndex]['Start'] = $TimeArray[$EventArray[$lessonIndex]['Title']]['Start'];
$lessonIndex++;
$EventArray[$lessonIndex]['Title'] = 'Lesson 3';
$EventArray[$lessonIndex]['Start'] = $TimeArray[$EventArray[$lessonIndex]['Title']]['Start'];
$lessonIndex++;
Everything works as expected.
You should not use a variable that gets incremented more than once in a line, else you risk this kind of unexpected behaviour.
What happens is that
=) getsevaluated.
$lessonIndexwill get incremented.$lessonIndexhas already been incremented.
So this would be equal to writing