xref: /PHP-8.0/tests/lang/foreachLoop.001.phpt (revision f8d79582)
1--TEST--
2Foreach loop tests - basic loop with just value and key => value.
3--FILE--
4<?php
5
6$a = array("a","b","c");
7
8foreach ($a as $v) {
9    var_dump($v);
10}
11foreach ($a as $k => $v) {
12    var_dump($k, $v);
13}
14//check key and value after the loop.
15var_dump($k, $v);
16
17echo "\n";
18//Dynamic array
19foreach (array("d","e","f") as $v) {
20    var_dump($v);
21}
22foreach (array("d","e","f") as $k => $v) {
23    var_dump($k, $v);
24}
25//check key and value after the loop.
26var_dump($k, $v);
27
28echo "\n";
29//Ensure counter is advanced during loop
30$a=array("a","b","c");
31foreach ($a as $v);
32var_dump(current($a));
33$a=array("a","b","c");
34foreach ($a as &$v);
35var_dump(current($a));
36
37?>
38--EXPECT--
39string(1) "a"
40string(1) "b"
41string(1) "c"
42int(0)
43string(1) "a"
44int(1)
45string(1) "b"
46int(2)
47string(1) "c"
48int(2)
49string(1) "c"
50
51string(1) "d"
52string(1) "e"
53string(1) "f"
54int(0)
55string(1) "d"
56int(1)
57string(1) "e"
58int(2)
59string(1) "f"
60int(2)
61string(1) "f"
62
63string(1) "a"
64string(1) "a"
65