xref: /PHP-7.3/Zend/tests/arg_unpack/by_ref.phpt (revision ded3d984)
1--TEST--
2Argument unpacking with by-ref arguments
3--FILE--
4<?php
5
6error_reporting(E_ALL);
7
8function test1(&...$args) {
9    foreach ($args as &$arg) {
10        $arg++;
11    }
12}
13
14test1(...[1, 2, 3]);
15
16$array = [1, 2, 3];
17test1(...$array);
18var_dump($array);
19
20$array1 = [1, 2]; $array2 = [3, 4];
21test1(...$array1, ...$array2);
22var_dump($array1, $array2);
23
24function test2($val1, &$ref1, $val2, &$ref2) {
25    $ref1++;
26    $ref2++;
27}
28
29$array = [0, 0, 0, 0];
30test2(...$array);
31var_dump($array);
32
33$array1 = [1, 2]; $array2 = [4, 5];
34test1(...$array1, ...$array2);
35var_dump($array1, $array2);
36
37$a = $b = $c = $d = 0;
38$array = [0, 0, 0, 0];
39
40test2($a, ...$array);
41var_dump($a, $array);
42
43test2($a, $b, ...$array);
44var_dump($a, $b, $array);
45
46test2($a, $b, $c, ...$array);
47var_dump($a, $b, $c, $array);
48
49test2($a, $b, $c, $d, ...$array);
50var_dump($a, $b, $c, $d, $array);
51
52?>
53--EXPECT--
54array(3) {
55  [0]=>
56  int(2)
57  [1]=>
58  int(3)
59  [2]=>
60  int(4)
61}
62array(2) {
63  [0]=>
64  int(2)
65  [1]=>
66  int(3)
67}
68array(2) {
69  [0]=>
70  int(4)
71  [1]=>
72  int(5)
73}
74array(4) {
75  [0]=>
76  int(0)
77  [1]=>
78  int(1)
79  [2]=>
80  int(0)
81  [3]=>
82  int(1)
83}
84array(2) {
85  [0]=>
86  int(2)
87  [1]=>
88  int(3)
89}
90array(2) {
91  [0]=>
92  int(5)
93  [1]=>
94  int(6)
95}
96int(0)
97array(4) {
98  [0]=>
99  int(1)
100  [1]=>
101  int(0)
102  [2]=>
103  int(1)
104  [3]=>
105  int(0)
106}
107int(0)
108int(1)
109array(4) {
110  [0]=>
111  int(1)
112  [1]=>
113  int(1)
114  [2]=>
115  int(1)
116  [3]=>
117  int(0)
118}
119int(0)
120int(2)
121int(0)
122array(4) {
123  [0]=>
124  int(2)
125  [1]=>
126  int(1)
127  [2]=>
128  int(1)
129  [3]=>
130  int(0)
131}
132int(0)
133int(3)
134int(0)
135int(1)
136array(4) {
137  [0]=>
138  int(2)
139  [1]=>
140  int(1)
141  [2]=>
142  int(1)
143  [3]=>
144  int(0)
145}
146