xref: /PHP-8.2/Zend/tests/arrow_functions/001.phpt (revision f3e5bbe6)
1--TEST--
2Basic arrow function functionality check
3--FILE--
4<?php
5
6$foo = fn() => 1;
7var_dump($foo());
8
9$foo = fn($x) => $x;
10var_dump($foo(2));
11
12$foo = fn($x, $y) => $x + $y;
13var_dump($foo(1, 2));
14
15// Closing over $var
16$var = 4;
17$foo = fn() => $var;
18var_dump($foo());
19
20// Not closing over $var, it's a parameter
21$foo = fn($var) => $var;
22var_dump($foo(5));
23
24// Close over $var by-value, not by-reference
25$var = 5;
26$foo = fn() => ++$var;
27var_dump($foo());
28var_dump($var);
29
30// Nested arrow functions closing over variable
31$var = 6;
32var_dump((fn() => fn() => $var)()());
33var_dump((fn() => function() use($var) { return $var; })()());
34
35?>
36--EXPECT--
37int(1)
38int(2)
39int(3)
40int(4)
41int(5)
42int(6)
43int(5)
44int(6)
45int(6)
46