@@ -10,6 +10,32 @@ $root = $doc->documentElement;
10
10
11
11
$ i = 0 ;
12
12
13
+ /* Note that the list is live. The explanation for the output is as follows:
14
+ Before the loop we have the following (writing only the attributes):
15
+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
+
17
+ Now the loop starts, the current element is marked with a V. $i == 0:
18
+ V
19
+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
20
+ 1 gets printed. $i == 0, which is even, so 1 gets removed, which results in:
21
+ V
22
+ 2 3 4 5 6 7 8 9 10 11 12 13 14 15
23
+ Note that everything shifted to the left.
24
+ Because the list is live, the current element pointer still refers to the first index, which now corresponds to element with attribute 2.
25
+ Now the foreach body ends, which means we go to the next element, which is now 3 instead of 2.
26
+ V
27
+ 2 3 4 5 6 7 8 9 10 11 12 13 14 15
28
+ 3 gets printed. $i == 1, which is odd, so nothing happens and we move on to the next element:
29
+ V
30
+ 2 3 4 5 6 7 8 9 10 11 12 13 14 15
31
+ 4 gets printed. $i == 2, which is even, so 4 gets removed, which results in:
32
+ V
33
+ 2 3 5 6 7 8 9 10 11 12 13 14 15
34
+ Note again everything shifted to the left.
35
+ Now the foreach body ends, which means we go to the next element, which is now 6 instead of 5.
36
+ V
37
+ 2 3 5 6 7 8 9 10 11 12 13 14 15
38
+ 6 gets printed, etc... */
13
39
foreach ($ doc ->getElementsByTagName ('e ' ) as $ node ) {
14
40
print $ node ->getAttribute ('i ' ) . ' ' ;
15
41
if ($ i ++ % 2 == 0 )
0 commit comments