xref: /PHP-8.1/tests/func/003.phpt (revision 7aacc705)
1--TEST--
2General function test
3--FILE--
4<?php
5
6function a()
7{
8  echo "hey\n";
9}
10
11function b($i)
12{
13  echo "$i\n";
14}
15
16
17function c($i,$j)
18{
19  echo "Counting from $i to $j\n";
20  for ($k=$i; $k<=$j; $k++) {
21    echo "$k\n";
22  }
23}
24
25
26
27a();
28b("blah");
29a();
30b("blah","blah");
31c(7,14);
32
33a();
34
35
36function factorial($n)
37{
38  if ($n==0 || $n==1) {
39    return 1;
40  } else {
41    return factorial($n-1)*$n;
42  }
43}
44
45
46function factorial2($start, $n)
47{
48  if ($n<=$start) {
49    return $start;
50  } else {
51    return factorial2($start,$n-1)*$n;
52  }
53}
54
55
56for ($k=0; $k<10; $k++) {
57  for ($i=0; $i<=10; $i++) {
58    $n=factorial($i);
59    echo "factorial($i) = $n\n";
60  }
61}
62
63
64echo "and now, from a function...\n";
65
66function call_fact()
67{
68  echo "(it should break at 5...)\n";
69  for ($i=0; $i<=10; $i++) {
70    if ($i == 5) break;
71    $n=factorial($i);
72    echo "factorial($i) = $n\n";
73  }
74}
75
76function return4() { return 4; }
77function return7() { return 7; }
78
79for ($k=0; $k<10; $k++) {
80  call_fact();
81}
82
83echo "------\n";
84$result = factorial(factorial(3));
85echo "$result\n";
86
87$result=factorial2(return4(),return7());
88echo "$result\n";
89
90function andi($i, $j)
91{
92    for ($k=$i ; $k<=$j ; $k++) {
93        if ($k >5) continue;
94        echo "$k\n";
95    }
96}
97
98andi (3,10);
99?>
100--EXPECT--
101hey
102blah
103hey
104blah
105Counting from 7 to 14
1067
1078
1089
10910
11011
11112
11213
11314
114hey
115factorial(0) = 1
116factorial(1) = 1
117factorial(2) = 2
118factorial(3) = 6
119factorial(4) = 24
120factorial(5) = 120
121factorial(6) = 720
122factorial(7) = 5040
123factorial(8) = 40320
124factorial(9) = 362880
125factorial(10) = 3628800
126factorial(0) = 1
127factorial(1) = 1
128factorial(2) = 2
129factorial(3) = 6
130factorial(4) = 24
131factorial(5) = 120
132factorial(6) = 720
133factorial(7) = 5040
134factorial(8) = 40320
135factorial(9) = 362880
136factorial(10) = 3628800
137factorial(0) = 1
138factorial(1) = 1
139factorial(2) = 2
140factorial(3) = 6
141factorial(4) = 24
142factorial(5) = 120
143factorial(6) = 720
144factorial(7) = 5040
145factorial(8) = 40320
146factorial(9) = 362880
147factorial(10) = 3628800
148factorial(0) = 1
149factorial(1) = 1
150factorial(2) = 2
151factorial(3) = 6
152factorial(4) = 24
153factorial(5) = 120
154factorial(6) = 720
155factorial(7) = 5040
156factorial(8) = 40320
157factorial(9) = 362880
158factorial(10) = 3628800
159factorial(0) = 1
160factorial(1) = 1
161factorial(2) = 2
162factorial(3) = 6
163factorial(4) = 24
164factorial(5) = 120
165factorial(6) = 720
166factorial(7) = 5040
167factorial(8) = 40320
168factorial(9) = 362880
169factorial(10) = 3628800
170factorial(0) = 1
171factorial(1) = 1
172factorial(2) = 2
173factorial(3) = 6
174factorial(4) = 24
175factorial(5) = 120
176factorial(6) = 720
177factorial(7) = 5040
178factorial(8) = 40320
179factorial(9) = 362880
180factorial(10) = 3628800
181factorial(0) = 1
182factorial(1) = 1
183factorial(2) = 2
184factorial(3) = 6
185factorial(4) = 24
186factorial(5) = 120
187factorial(6) = 720
188factorial(7) = 5040
189factorial(8) = 40320
190factorial(9) = 362880
191factorial(10) = 3628800
192factorial(0) = 1
193factorial(1) = 1
194factorial(2) = 2
195factorial(3) = 6
196factorial(4) = 24
197factorial(5) = 120
198factorial(6) = 720
199factorial(7) = 5040
200factorial(8) = 40320
201factorial(9) = 362880
202factorial(10) = 3628800
203factorial(0) = 1
204factorial(1) = 1
205factorial(2) = 2
206factorial(3) = 6
207factorial(4) = 24
208factorial(5) = 120
209factorial(6) = 720
210factorial(7) = 5040
211factorial(8) = 40320
212factorial(9) = 362880
213factorial(10) = 3628800
214factorial(0) = 1
215factorial(1) = 1
216factorial(2) = 2
217factorial(3) = 6
218factorial(4) = 24
219factorial(5) = 120
220factorial(6) = 720
221factorial(7) = 5040
222factorial(8) = 40320
223factorial(9) = 362880
224factorial(10) = 3628800
225and now, from a function...
226(it should break at 5...)
227factorial(0) = 1
228factorial(1) = 1
229factorial(2) = 2
230factorial(3) = 6
231factorial(4) = 24
232(it should break at 5...)
233factorial(0) = 1
234factorial(1) = 1
235factorial(2) = 2
236factorial(3) = 6
237factorial(4) = 24
238(it should break at 5...)
239factorial(0) = 1
240factorial(1) = 1
241factorial(2) = 2
242factorial(3) = 6
243factorial(4) = 24
244(it should break at 5...)
245factorial(0) = 1
246factorial(1) = 1
247factorial(2) = 2
248factorial(3) = 6
249factorial(4) = 24
250(it should break at 5...)
251factorial(0) = 1
252factorial(1) = 1
253factorial(2) = 2
254factorial(3) = 6
255factorial(4) = 24
256(it should break at 5...)
257factorial(0) = 1
258factorial(1) = 1
259factorial(2) = 2
260factorial(3) = 6
261factorial(4) = 24
262(it should break at 5...)
263factorial(0) = 1
264factorial(1) = 1
265factorial(2) = 2
266factorial(3) = 6
267factorial(4) = 24
268(it should break at 5...)
269factorial(0) = 1
270factorial(1) = 1
271factorial(2) = 2
272factorial(3) = 6
273factorial(4) = 24
274(it should break at 5...)
275factorial(0) = 1
276factorial(1) = 1
277factorial(2) = 2
278factorial(3) = 6
279factorial(4) = 24
280(it should break at 5...)
281factorial(0) = 1
282factorial(1) = 1
283factorial(2) = 2
284factorial(3) = 6
285factorial(4) = 24
286------
287720
288840
2893
2904
2915
292