xref: /PHP-7.3/tests/lang/operators/coalesce.phpt (revision e5eb9530)
1--TEST--
2Test ?? operator
3--FILE--
4<?php
5
6$var = 7;
7$var2 = NULL;
8
9$obj = new StdClass;
10$obj->boo = 7;
11
12$arr = [
13	2 => 7,
14	"foo" => "bar",
15	"foobar" => NULL,
16	"qux" => $obj,
17	"bing" => [
18		"bang"
19	]
20];
21
22function foobar() {
23	echo "called\n";
24	return ['a'];
25}
26
27var_dump($nonexistent_variable ?? 3);
28echo PHP_EOL;
29var_dump($var ?? 3);
30var_dump($var2 ?? 3);
31echo PHP_EOL;
32var_dump($obj->boo ?? 3);
33var_dump($obj->bing ?? 3);
34var_dump($arr["qux"]->boo ?? 3);
35var_dump($arr["qux"]->bing ?? 3);
36echo PHP_EOL;
37var_dump($arr[2] ?? 3);
38var_dump($arr["foo"] ?? 3);
39var_dump($arr["foobar"] ?? 3);
40var_dump($arr["qux"] ?? 3);
41var_dump($arr["bing"][0] ?? 3);
42var_dump($arr["bing"][1] ?? 3);
43echo PHP_EOL;
44var_dump(foobar()[0] ?? false);
45echo PHP_EOL;
46function f($x)
47{
48    printf("%s(%d)\n", __FUNCTION__, $x);
49    return $x;
50}
51
52$a = f(null) ?? f(1) ?? f(2);
53?>
54--EXPECTF--
55int(3)
56
57int(7)
58int(3)
59
60int(7)
61int(3)
62int(7)
63int(3)
64
65int(7)
66string(3) "bar"
67int(3)
68object(stdClass)#%d (%d) {
69  ["boo"]=>
70  int(7)
71}
72string(4) "bang"
73int(3)
74
75called
76string(1) "a"
77
78f(0)
79f(1)
80