forked from solvery/lang-features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_1.php
More file actions
95 lines (71 loc) · 1.12 KB
/
function_1.php
File metadata and controls
95 lines (71 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
function add3($x1, $x2, $x3)
{
return $x1 + $x2 + $x3;
}
add3(1, 2, 3);
# function names are case insensitive:
ADD3(1, 2, 3);
function my_log($x, $base=10)
{
return log($x) / log($base);
}
my_log(42);
my_log(42, M_E);
function first_and_last()
{
$arg_cnt = func_num_args();
if ($arg_cnt >= 1) {
$n = func_get_arg(0);
echo "first: " . $n . "\n";
}
if ($arg_cnt >= 2) {
$a = func_get_args();
$n = $a[$arg_cnt-1];
echo "last: " . $n . "\n";
}
}
$a = [1, 2, 3];
call_user_func_array("add3", $a);
function first_and_second(&$a)
{
return [$a[0], $a[1]];
}
$a = [1, 2, 3];
list($x, $y) =
first_and_second($a);
$sqr = function ($x) {
return $x * $x;
};
$sqr(2);
$func = "add";
function counter()
{
static $i = 0;
return ++$i;
}
echo counter();
function make_counter()
{
$i = 0;
return function () use (&$i) {
return ++$i;
};
}
$nays = make_counter();
echo $nays();
# PHP 5.5:
function make_counter2() {
$i = 0;
while (1) {
yield ++$i;
}
}
$nays = make_counter2();
# does not return a value:
$nays->next();
# runs generator if generator has not
# yet yielded:
echo $nays->current();
echo "\n";
?>