File: control-structures.xml

package info (click to toggle)
phpdoc 20020310-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 35,272 kB
  • ctags: 354
  • sloc: xml: 799,767; php: 1,395; cpp: 500; makefile: 200; sh: 140; awk: 51
file content (1453 lines) | stat: -rw-r--r-- 50,526 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- up-to-date against phpdoc/en/language/control-structures.xml:1.22 
   (t/m regel 526 tot 1.33, en de declare section toegevoegd) -->
 <chapter id="control-structures">
  <title>Control Structures</title>

  <simpara>
   Elk PHP script bestaat uit reeksen van statements. Een statement
   kan een assignment, een functie aanroep, een loop, een conditional
   statement of zelfs een statement zijn dat niets doet (een empty
   statement). Statements eindigen gewoonlijk met een puntkomma.
   Als toevoeging hierop kunnen statements gegroepeerd worden tot een
   statement-groep door deze te omvatten met accolades. Een statement-groep
   is een statement op zichzelf. De overige statements zijn beschreven in
   dit hoofdstuk.
  </simpara>

  <sect1 id="control-structures.if">
   <title><literal>if</literal></title>
   <para>
    De <literal>if</literal> constructie is &eacute;&eacute;n van de
    meest belangrijkste in veel talen, zoals ook in PHP. Het maakt het
    mogelijk om stukken code conditioneel uit te voeren. PHP heeft een
    <literal>if</literal> structuur die gelijk is als die van de taal C.
    <informalexample>
     <programlisting>
if (expressie)
    statement
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Zoals is beschreven in de sectie over expressies, wordt
    &quot;expressie&quot; geevalueerd naar zijn &quot;truth&quot; waarde.
    Als de <replaceable>expressie</replaceable>
    evalueert tot &true;, dan zal PHP het statement uitvoeren,
    en als de expressie evalueert naar &false;, dan zal het
    statement worden overgeslagen.
   </simpara>
   <para>
    Het volgende voorbeeld zal bijvoorbeeld tonen <computeroutput>a is groter
    dan b</computeroutput> indien <replaceable>$a</replaceable> groter is
    dan <replaceable>$b</replaceable>:
    <informalexample>
     <programlisting role="php">
if ($a &gt; $b)
    print &quot;a is groter dan b&quot;;
     </programlisting>
    </informalexample>
   </para>
   <para>
    Vaak zul je willen dat er meer dan &eacute;&eacute;n statement
    conditioneel wordt uitgevoerd. Natuurlijk hoef je niet elk
    statement met een <literal>if</literal> clause te omvatten. In
    plaats daarvan kun je een meerdere statements tot een
    statement groep maken. De code in het volgende voorbeeld zal
    tonen <computeroutput>a is groter dan b</computeroutput>
    als <replaceable>$a</replaceable> groter is dan
    <replaceable>$b</replaceable>, en het zal de waarde van de variabele
    <replaceable>$a</replaceable> naar <replaceable>$b</replaceable>
    kopie&euml;ren:
    <informalexample>
     <programlisting role="php">
if ($a &gt; $b) {
    print &quot;a is groter dan b&quot;;
    $b = $a;
}
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    If statements kunnen oneindig worden genest binnen andere
    <literal>if</literal> statements. Dit geeft je de complete flexibiliteit
    voor conditionele uitvoering van verschillende delen van je programma.
   </simpara>
  </sect1>
 
  <sect1 id="control-structures.else">
   <title><literal>else</literal></title>
   <para>
    Vaak zul je &eacute;&eacute;n statement willen uitvoeren als er aan een
    bepaalde voorwaarde is voldaan, en een ander statement als er niet
    aan de voorwaarde is voldaan. Hierom bestaat <literal>else</literal>.
    <literal>else</literal> breidt een if statement uit met de mogelijkheid
    om een statement uit te voeren als de expressie in de
    <literal>if</literal> evalueerd tot &false;. Het
    onderstaande voorbeeld zal tonen <computeroutput>a is groter dan
    b</computeroutput> als <replaceable>$a</replaceable> groter is dan
    <replaceable>$b</replaceable>, en <computeroutput>a is NIET groter
    dan b</computeroutput> indien <replaceable>$a</replaceable> niet groter
    is dan <replaceable>$b</replaceable>.
    <informalexample>
     <programlisting role="php">
if ($a &gt; $b) {
    print &quot;a is groter dan b&quot;;
} else {
    print &quot;a is NIET groter dan b&quot;;
}
     </programlisting>
    </informalexample>

    Het <literal>else</literal> statement wordt alleen maar uitgevoerd als
    de <literal>if</literal> expressie evalueerd tot
    &false;, en als alle mogelijke
    <literal>elseif</literal> expressies ook tot
    &false; evalueren (zie <link
    linkend="control-structures.elseif">elseif</link>).

   </para>
  </sect1>
 
  <sect1 id="control-structures.elseif">
   <title><literal>elseif</literal></title>
   <para>
    <literal>elseif</literal>, zoals de naam al suggereerd is een combinatie
    van de <literal>if</literal> en de <literal>else</literal>. Net zoals
    de <literal>else</literal>, breidt het de <literal>if</literal>
    uit met de mogelijkheid om een ander statement uit te voeren als de
    <literal>if</literal> expressie evalueerd naar
    &false;. In tegenstelling tot
    <literal>else</literal> zal <literal>elseif</literal> het statement
    alleen uitvoeren als de <literal>elseif</literal> expressie evalueerd
    tot &true;. Als voorbeeld zal de code
    hieronder <computeroutput>a is groter dan b</computeroutput>,
    <computeroutput>a is gelijk aan b</computeroutput>
    of <computeroutput>a is kleiner dan b</computeroutput> tonen:
    <informalexample>
     <programlisting role="php">
if ($a &gt; $b) {
    print &quot;a is groter dan b&quot;;
} elseif ($a == $b) {
    print &quot;a is gelijk aan b&quot;;
} else {
    print &quot;a is kleiner dan b&quot;;
}
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Je kunt meerdere <literal>elseif</literal>s binnen hetzelfde
    <literal>if</literal> statement gebruiken. De eerste
    <literal>elseif</literal> expressie (indien aanwezig) die evalueerd
    tot &true; zal worden uitgevoerd. In PHP kun je ook
    gebruken 'else if' (in twee woorden) en de werking zou precies identiek
    zijn aan &eacute;&eacute;n 'elseif' (in &eacute;&eacute;n woord).
    De syntactische betekenis is heel iets anders (net zoals in C),
    maar beide zullen precies hetzelfde resultaat opleveren.
   </simpara>
   <simpara>
    Het <literal>elseif</literal> statement wordt alleen maar
    uitgevoerd indien alle voorgaande <literal>if</literal>
    expressies &eacute;n alle voorafgaande
    <literal>elseif</literal> expressies evalueren tot 
    &false;, en de huidige
    <literal>elseif</literal> expressie evalueerd tot
    &true;.
   </simpara>
  </sect1>
 
  <sect1 id="control-structures.alternative-syntax">
   <title>Alternatieve syntax voor control structures</title>
   <para>
    <warning>
     <simpara>
      Alternative syntax is deprecated as of PHP 4. Basically,
      it just generates unreadable code, and it gets
      very complicated when mixing it with the normal syntax.
      Although there are no plans to break this syntax, it
      cannot be ruled out that one day this will stop working.
      You are warned.
     </simpara>
    </warning>
   </para>
   <para>
    PHP bied een alternative syntax voor sommige control structures;
    namelijk voor <literal>if</literal>, <literal>while</literal>,
    <literal>for</literal>, <literal>foreach</literal> en
    <literal>switch</literal>. De alternatieve syntax verkrijg je
    door het vervangen van de openingsaccolade door een dubbele
    punt (:) en de sluit accolade door respectivelijk
    <literal>endif;</literal>, <literal>endwhile;</literal>,
    <literal>endfor;</literal>, <literal>endforeach;</literal> of
    <literal>endswitch;</literal>.
    <informalexample>
     <programlisting role="php">
 &lt;?php if ($a == 5): ?&gt;
 A is gelijk aan 5
 &lt;?php endif; ?&gt;
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    In het bovenstaande voorbeeld is het HTML block &quot;A is gelijk
    aan 5&quot; genest binnen een <literal>if</literal> statement,
    dat geschreven is in de alternative syntax. Het HTML blok wordt
    alleen getoond indien $a gelijk is aan 5.
   </simpara>
   <para>
    De alternatieve syntax is ook te gebruiken voor <literal>else</literal>
    en <literal>elseif</literal>. Het volgende is een 
    <literal>if</literal> structuur met <literal>elseif</literal> en
    <literal>else</literal> in het alternatieve formaat:
    <informalexample>
     <programlisting role="php">
if ($a == 5):
    print &quot;a is gelijk aan 5&quot;;
    print &quot;...&quot;;
elseif ($a == 6):
    print &quot;a is gelijk aan 6&quot;;
    print &quot;!!!&quot;;
else:
    print &quot;a is niet gelijk aan 5 of 6&quot;;
endif;
     </programlisting>
    </informalexample>
   </para>
   <para>
    Zie ook <link linkend="control-structures.while">while</link>,
    <link linkend="control-structures.for">for</link>, en <link
    linkend="control-structures.if">if</link> voor meer voorbeelden.
   </para>
  </sect1>

  <sect1 id="control-structures.while">
   <title><literal>while</literal></title>
   <para>
    <literal>while</literal> loops zijn de eenvoudigste manier van loops
    in PHP. De gedragen precies gelijk aan de while loops in C. De basis
    vorm van een <literal>while</literal> statement is:
    <informalexample>
     <programlisting>
while (expressie) statement
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    De bedoeling van het <literal>while</literal> statement is eenvoudig.
    Het laat PHP de geneste statements herhaaldelijk uitvoeren, zolang
    als de <literal>while</literal> expressie evaluateerd tot
    &true;. De waarde van de expressie wordt elke keer
    aan het begin van de loop gecontroleerd, dus zelfs als deze waarde
    veranderd tijdens het uitvoeren van de geneste statement(s), zal de
    uitvoer niet stoppen voordat de iteratie is afgelopen (elke keer dat
    PHP de statements in de loop uitvoert is &eacute;&eacute;n iteratie).
    Als wanneer voor de eerste keer de <literal>while</literal>
    expressie evaluateerd tot &false;, dan worden de
    statement(s) zelfs niet &eacute;&eacute;n keer uitgevoerd.
   </simpara>
   <para>
    Net zoals met het <literal>if</literal> statement, kun je meerdere
    statements uitvoeren binnen dezelfde <literal>while</literal> loop,
    door een groep van statements te omvatten met accolades, of door de
    alternatieve syntax te gebruiken:
    <informalexample>
     <programlisting>
while (expressie): statement ... endwhile;
     </programlisting>
    </informalexample>
   </para>
   <para>
    De volgende voorbeelden zijn identiek en zullen beide de getallen
    van 1 tot 10 laten zien:
    <informalexample>
     <programlisting>
/* voorbeeld 1 */

$i = 1;
while ($i &lt;= 10) {
    print $i++;  /* De waarde wordt getoond voordat deze 
                    wordt opgehoogt (post-increment) */
}
 
/* voorbeeld 2 */
 
$i = 1;
while ($i &lt;= 10):
    print $i;
    $i++;
endwhile;
     </programlisting>
    </informalexample>
   </para>
  </sect1>
 
  <sect1 id="control-structures.do.while">
   <title><literal>do..while</literal></title>
   <simpara>
    <literal>do..while</literal> loops lijken erg veel op
    <literal>while</literal> loops, alleen wordt de vergelijking nu 
    gecontroleerd aan het einde van de interatie, in plaats van aan het
    begin. Het grootste verschil met normale <literal>while</literal>
    loops is dat er altijd minimaal &eacute;&eacute;n iteratie van een
    <literal>do..while</literal> loop wordt uitgevoerd. De expressie
    wordt namelijk pas aan het einde van de iteratie geevalueerd,
    dit in tegenstelling tot de normale <literal>while</literal> loop,
    waar de expressie aan het begin van een iteratie wordt geevalueerd,
    Als de expressie meteen al aan het begin evauleerd tot
    &false;, dan zal de loop niet &eacute;&eacute;n 
    keer worden uitgevoerd.
   </simpara>
   <para>
    Er is maar &eacute;&eacute;n systax voor
	<literal>do..while</literal> loops:
 
    <informalexample>
     <programlisting role="php">
$i = 0;
do {
   print $i;
} while ($i &gt; 0);
     </programlisting>
    </informalexample>
   </para>
   <simpara>
     De bovenstaande loop wordt exact &eacute;&eacute;n keer uitgevoerd.
     Als na de eerste iteratie, wanneer de expressie wordt geevalueerd,
     de waarheids expressie evalueerd tot &false;
     ($i is niet groter dan 0) dan eindigd de loop.
   </simpara>
   <para>
    Gevorderde C programmeurs zullen bekend zijn met een ander gebruik
    van de <literal>do..while</literal> loop, namelijk om de executie
	in het midden van een blok code to stoppen. Dit kan door een
    groep statements te omvatten met <literal>do..while</literal>(0),
    en dan het
    <link linkend="control-structures.break"><literal>break</literal></link>
    statement te gebruiken om naar het einde van de loop te springen.
    Het volgende voorbeeld demonstreert dit:
    <informalexample>
     <programlisting role="php">
do {
    if ($i &lt; 5) {
        print &quot;i is niet groot genoeg&quot;;
        break;
    }
    $i *= $factor;
    if ($i &lt; $minimum_limiet) {
        break;
    }
    print &quot;i is ok&quot;;

     ...doe wat met i...

} while(0);
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Vrees niet als je dit niet meteen bergijpt, of helemaal niet. Je
    kunt zeer complexe PHP scripts schrijven zonder gebruik te maken van
    deze `feature'.
   </simpara>
  </sect1>
 
  <sect1 id="control-structures.for">
   <title><literal>for</literal></title>
   <para>
    <literal>for</literal> loops zijn de meest complexe loops in PHP.
    Ze gedragen zich net als de for-loops in C. De syntax van een
    <literal>for</literal> loop is:
    <informalexample>
     <programlisting>
for (expr1; expr2; expr3) statement
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    De eerste expressie (<replaceable>expr1</replaceable>) wordt
    exact &eacute;&eacute;n keer uitgevoerd aan het begin van de loop.
   </simpara>
   <simpara>
    Aan het begin van elke iteratie wordt
    <replaceable>expr2</replaceable> geevalueerd. Als deze evalueert
    tot &true;, dan gaat de loop verder en worden de
    geneste statements uitgevoerd. De execute van de loop stopt
    als de expressie evalueert tot &false;.
   </simpara>
   <simpara>
    Aan het einde van elke iteratie wordt <replaceable>expr3</replaceable>
    uitgevoerd.
   </simpara>
   <simpara>
    Elk van de drie expressies mag leeg zijn. Als
    <replaceable>expr2</replaceable> leeg is dan betekend dat dat de
    loop oneindig zal lopen (PHP neemt aan dat als de tweede expressie
    leeg is, de expressie de waarde &true; heeft, net als
    in C). Dit is niet zo nutteloos als je zult denken, het is immers
    mogelijk dat de loop wordt onderbroken met een conditioneel <link
    linkend="control-structures.break"><literal>break</literal></link>
    statement in plaats van het gebruik van een waarheidsexpressie.
   </simpara>
   <para>
    Neem de volgende voorbeelden. Elk van deze zal de nummers 1 tot 10
    afdrukken:
    <informalexample>
     <programlisting role="php">
/* voorbeeld 1 */
 
for ($i = 1; $i &lt;= 10; $i++) {
    print $i;
}
 
/* voorbeeld 2 */
 
for ($i = 1;;$i++) {
    if ($i &gt; 10) {
        break;
    }
    print $i;
}
 
/* voorbeeld 3 */
 
$i = 1;
for (;;) {
    if ($i &gt; 10) {
        break;
    }
    print $i;
    $i++;
}
 
/* voorbeeld 4 */
 
for ($i = 1; $i &lt;= 10; print $i, $i++) ;
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Natuurlijk lijkt het eerste voorbeeld het netste (of misschien het
    vierde), maar je zult zien dat het leeglaten van expressie in
    de <literal>for</literal> loops toch vaak handig kan zijn.
   </simpara>
   <para>
    PHP heeft ook een &quot;dubbele punt syntax&quot; voor
    <literal>for</literal> loops.
    <informalexample>
     <programlisting>
for (expr1; expr2; expr3): statement; ...; endfor;
     </programlisting>
     </informalexample>
   </para>
   <para>
    Andere talen hebben een <literal>foreach</literal> statement voor
    het doorlopen van een array of hash. PHP 3 heeft niet zo'n
    constructie; PHP 4 heeft deze wel (zie <link
    linkend="control-structures.foreach">foreach</link>). In PHP 3 kun
    je hetzelfde resultaat bereiken door een
    <link linkend="control-structures.while">while</link> met een
    <function>list</function> en een <function>each</function>
    functie te combineren. Zie de documentatie bij deze functies
    voor een voorbeeld.
   </para>

  </sect1>
  <sect1 id="control-structures.foreach">
   <title><literal>foreach</literal></title>
   <para>
    PHP 4 (maar PHP 3 niet) heeft een <literal>foreach</literal> construct,
    dat veel op die van PERL en sommige andere talen lijkt. Dit construct
    geeft een makkelijke manier om door een array te lopen. Er zijn twee
    syntaxes, de tweede is een kleine, maar handige, uitbreiding op de
    eerste:
    <informalexample>
     <programlisting>
foreach(array_expressie as $waarde) statement
foreach(array_expressie as $key =&gt; $waarde) statement
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    De eerste vorm loopt door de array, aangegeven met
    <literal>array_expressie</literal>. In elke iteratie wordt de waarde
    van het huidige element in <literal>$waarde</literal> geplaatst en
    wordt de interne array pointer &eacute;&eacute;n positie verder gezet
    (zodat in de volgende iteratie het volgende element wordt opgehaald).
   </simpara>
   <simpara>
    De tweede vorm werkt precies zo, alleen zal de key van het element
    ook nog eens aan de variabele <literal>$key</literal> gekoppeld.
   </simpara>
   <para>
    <note>
     <para>
      Als <literal>foreach</literal> begint met het doorlopen van de array,
      dan wordt de interne array pointer automatisch gereset naar het
      eerste element van de array. Dit betekend dat je niet eerst
      <function>reset</function> hoeft te gebruiken voor een
      <literal>foreach</literal> loop.
     </para>
    </note>
   </para>
   <para>
    <note>
     <para>
      Also note that <literal>foreach</literal> operates on a copy of
      the specified array, not the array itself, therefore the array
      pointer is not modified as with the <function>each</function>
      construct and changes to the array element returned are not
      reflected in the original array.
     </para>
    </note>
   </para>
   <note>
    <para>
     <literal>foreach</literal> does not support the ability to
     suppress error messages using '@'.
    </para>
   </note>
   <para>
    De volgende twee voorbeelden zijn functioneel identiek aan elkaar:
    <informalexample>
     <programlisting role="php">
reset ($arr);
while (list(, $value) = each ($arr)) {
    echo &quot;Waarde: $value&lt;br&gt;\n&quot;;
}

foreach ($arr as $value) {
    echo &quot;Waarde: $value&lt;br&gt;\n&quot;;
}
     </programlisting>
    </informalexample>
    De volgende zijn ook functioneel aan elkaar:
    <informalexample>
     <programlisting role="php">
reset ($arr);
while (list($key, $value) = each ($arr)) {
    echo &quot;Key: $key; Waarde: $value&lt;br&gt;\n&quot;;
}

foreach ($arr as $key =&gt; $value) {
    echo &quot;Key: $key; Waarde: $value&lt;br&gt;\n&quot;;
}
     </programlisting>
    </informalexample>
   </para>
   <para>
    Hier volgen nog wat meer voorbeelden van het gebruik van deze
    functie:
    <informalexample>
     <programlisting role="php">
/* foreach voorbeeld 1: alleen de waarde */

$a = array (1, 2, 3, 17);

foreach ($a as $v) {
   print &quot;Huidige waarde van \$a: $v.\n&quot;;
}

/* foreach voorbeeld 2: waarde (samen met de key ter illustratie) */

$a = array (1, 2, 3, 17);

$i = 0; /* alleen ter illustratie */

foreach($a as $v) {
    print &quot;\$a[$i] =&gt; $k.\n&quot;;
}

/* foreach voorbeeld 3: key en waarde */

$a = array (
    "een" =&gt; 1,
    "twee" =&gt; 2,
    "drie" =&gt; 3,
    "zeventien" =&gt; 17
);

foreach($a as $k =&gt; $v) {
    print "\$a[$k] =&gt; $v.\n";
}
     </programlisting>
    </informalexample>
   </para>
  </sect1>
 
  <sect1 id="control-structures.break">
   <title><literal>break</literal></title>
   <simpara>
    <literal>break</literal> stopt de uitvoering van de huidige
    <literal>for</literal>, <literal>while</literal>, of
    <literal>switch</literal> structuur.
   </simpara>
   <simpara>
    <literal>break</literal> accepteert een optioneel numeriek argument
    waarmee wordt aangegeven uit hoeveel geneste blokken moet worden
    teruggekeerd. 
   </simpara>
   <para>
    <informalexample>
     <programlisting role="php">
$arr = array ('een', 'twee', 'drie', 'vier', 'stop', 'vijf');
while (list (, $val) = each ($arr)) {
    if ($val == 'stop') {
        break;    /* Je had hier ook kunnen schrijven: 'break 1;' */
    }
    echo "$val&lt;br&gt;\n";
}

/* Gebruikmakend van het optionele argument. */

$i = 0;
while (++$i) {
    switch ($i) {
    case 5:
        echo "Op 5&lt;br&gt;\n";
        break 1;  /* Breek alleen uit de switch. */
    case 10:
        echo "Op 10: stoppen&lt;br&gt;\n";
        break 2;  /* Breek uit de switch en de while. */
    default:
        break;
    }
}
     </programlisting>
    </informalexample>
   </para>
  </sect1>
 
  <sect1 id="control-structures.continue">
   <title><literal>continue</literal></title>
   <simpara>
    <literal>continue</literal> wordt binnen loop structuren gebruikt om
    de rest van de loop niet uit te voeren, en vervolgens de loop met
    een nieuwe iteratie laten te beginnen.
   </simpara>
   <simpara>
    <literal>continue</literal> accepteert een optioneel argument dat
    aangeeft voor hoeveel levels van geneste loops naar het einde moet
    worden gesprongen.
   </simpara>
   <para>
    <informalexample>
     <programlisting role="php">
while (list ($key, $value) = each ($arr)) {
    if (!($key % 2)) { // sla oneven cijfers over
        continue;
    }
    doe_iets_nuttigs ($value);
}

$i = 0;
while ($i++ &lt; 5) {
    echo "Outer&lt;br&gt;\n";
    while (1) {
        echo "&nbsp;&nbsp;Middle&lt;br&gt;\n";
        while (1) {
            echo "&nbsp;&nbsp;Inner&lt;br&gt;\n";
            continue 3;
        }
        echo "Dit wordt nooit uitgevoerd&lt;br&gt;\n";
    }
    echo "En dit ook niet.&lt;br&gt;\n";
}
     </programlisting>
     </informalexample>
    </para>
  </sect1>
 
  <sect1 id="control-structures.switch">
   <title><literal>switch</literal></title>
   <simpara>
    Het <literal>switch</literal> statement is gelijk aan een serie van
    IF statements met dezelfde expressie. Op veel plaatsen zul je dezelfde
    variabele (of expressie) willen vergelijken met meerdere waardes,
    en zul je een stuk code willen uitvoeren, dat afhangt
    van de waarde waarmee de expressie overeenkomt. Dit is precies waar
    het <literal>switch</literal> statement voor is.
   </simpara> 
   <para>
    De volgende twee voorbeelden zijn twee verschillende manieren om
    hetzelfde te doen, de &eacute;&eacute;n met een serie
    <literal>if</literal> statements, de andere met behulp
    van het <literal>switch</literal> statement:
    <informalexample>
     <programlisting role="php">
if ($i == 0) {
    print "i is gelijk aan 0";
}
if ($i == 1) {
    print "i is gelijk aan 1";
}
if ($i == 2) {
    print "i is gelijk aan 2";
}
 
switch ($i) {
    case 0:
        print "i is gelijk aan 0";
        break;
    case 1:
        print "i is gelijk aan 1";
        break;
    case 2:
        print "i is gelijk aan 2";
        break;
}
     </programlisting>
    </informalexample>
   </para>
   <para>
    Het is belangrijk om te begrijpen hoe het <literal>switch</literal>
    statement precies werkt om fouten te voorkomen. Het
    <literal>switch</literal> statement voert regel voor regel
    (eigenelijk, statement voor statement) uit. In het begin wordt er geen
    code uitgevoerd. Alleen als er een <literal>case</literal> statement
    wordt gevonden met een waarde die overeenkomt met de waarde van de
    <literal>switch</literal> expressie begint PHP met het uitvoeren van
    statements. PHP gaat net zolang door met het uitvoeren van statements
    tot het einde van het <literal>switch</literal> blok, of totdat het
    voor de eerste keer een <literal>break</literal> statement tegenkomt.
    Als je geen <literal>break</literal> statement gebruikt aan het einde
    van de statement list die bij de &quot;case&quot; hoort, zal PHP
    doorgaan met het uitvoeren van statements die bij volgende cases
    horen. Zie het volgende voorbeeld:
    <informalexample>
     <programlisting role="php">
switch ($i) {
    case 0:
        print "i is gelijk aan 0";
    case 1:
        print "i is gelijk aan 1";
    case 2:
        print "i is gelijk aan 2";
}
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Als in het bovenstaande voorbeeld $i gelijk is aan 0, zal PHP 
    alle print statements uitvoeren! Als $i gelijk is aan 1, dan
    zal PHP de laatste twee print statements uitvoeren. Alleen als
    $i gelijk is aan 2, krijg je het 'gewenste' resultaat, dan wordt
    namelijk alleen het laaste print statement uitgevoerd. Het is dus
    belangrijk om geen <literal>break</literal> statements te vergeten.
    (Onder bepaalde omstandigeheden kan het weglaten van de break
    statements juist wel nuttig zijn, zoals het voorbeeld hieronder
    laat zien.
   </simpara>
   <simpara>
    In een <literal>switch</literal> statement, wordt de conditie
    maar &eacute;&eacute;n keer geevalueerd, vervolgends wordt de
    uitkomst van deze evaluatie vergeleken met de verschillende
    <literal>case</literal> statements. Bij een <literal>elseif</literal>
    statement wordt de conditie per elseif geevalueerd. Dit betekent dat
    je, als je een gecompliceerde evaluatie hebt, het gebruik van een
    <literal>switch</literal> statement wellicht sneller zou werken.
   </simpara>
   <para>
    De statement list voor een case kan ook leeg zijn, op deze manier
    zal de controle meteen worden doorgegeven aan de volgende case:
    <informalexample>
     <programlisting role="php">
switch ($i) {
    case 0:
    case 1:
    case 2:
        print "i is kleiner dan 3 maar niet negatief";
        break;
    case 3:
        print "i is 3";
}
     </programlisting>
    </informalexample>
   </para>
   <para>
    De default case is een speciale. Deze case matched met alles dat
    niet is gematched door eerdere cases. Bijvoorbeeld:
    <informalexample>
     <programlisting role="php">
switch ($i) {
    case 0:
        print "i is gelijk aan 0";
        break;
    case 1:
        print "i is gelijk aan 1";
        break;
    case 2:
        print "i is gelijk aan 2";
        break;
    default:
        print "i is niet gelijk aan 0, 1 of 2";
}
     </programlisting>
    </informalexample>
   </para>
   <para>
    De <literal>case</literal> expressie mag elke expressie zijn die
    evalueerd toe een simpel type, dit zijn integers, floating-point
    getallen en strings. Array's en objecten kunnen hier niet voor
    worden gebruikt.
   </para>
   <para>
    De alternative syntax voor control structures wordt ook ondersteund
    voor switches. Voor meer informatie, zie <link
    linkend="control-structures.alternative-syntax">Alternatieve syntax
    voor control structures</link> .
    <informalexample>
     <programlisting role="php">
switch ($i):
    case 0:
        print "i is gelijk aan 0";
        break;
    case 1:
        print "i is gelijk aan 1";
        break;
    case 2:
        print "i is gelijk aan 2";
        break;
    default:
        print "i is niet gelijk aan 0, 1 of 2";
endswitch;
     </programlisting>
    </informalexample>
   </para>
  </sect1>

  <sect1 id="control-structures.declare">
   <title><literal>declare</literal></title>
   <para>
    The <literal>declare</literal> construct is used to is
    used to set execution directives for a block of code.
    The syntax of <literal>declare</literal> is similiar to
    the syntax of other flow control constructs:
    <informalexample>
     <programlisting>
declare (directive) statement
     </programlisting>
    </informalexample>
   </para>
   <para>
    The <literal>directive</literal> section allows the
    behavior of the <literal>declare</literal> block to
    be set.
    Currently only one directive is recognized: the
    <literal>ticks</literal> directive. (See below for more
    information on the
    <link linkend="control-structures.declare.ticks">ticks</link>
    directive)
   </para>
   <para>
    The <literal>statement</literal> part of the
    <literal>declare</literal> block will be executed - how
    it is executed and what side-effects occur during execution
    may depend on the directive set in the
    <literal>directive</literal> block.
   </para>
   <sect2 id="control-structures.declare.ticks">
    <title>Ticks</title>
    <para>A tick is an event that occurs for every
    <replaceable>N</replaceable> low-level statements executed
    by the parser within the <literal>declare</literal> block.
    The value for <replaceable>N</replaceable> is specified
    using <literal>ticks=<replaceable>N</replaceable></literal>
    within the <literal>declare</literal> blocks's
    <literal>directive</literal> section.
   </para>
   <para>
    The event(s) that occurs on each tick is specified using the
    <function>register_tick_function</function>. See the example 
    below for more details. Note that more than one event can occur
    for each tick.
   </para>
   <para>
    <example>
     <title>Profile a section of PHP code</title>
     <programlisting role="php">
&lt;pre&gt;
&lt;?php
// A function that records the time when it is called
function profile ($dump = FALSE)
{
    static $profile;

    // Return the times stored in profile, then erase it
    if ($dump) {
        $temp = $profile;
        unset ($profile);
        return ($temp);
    }

    $profile[] = microtime ();
}

// Set up a tick handler
register_tick_function("profile");

// Initialize the function before the declare block
profile ();

// Run a block of code, throw a tick every 2nd statement
declare (ticks=2) {
    for ($x = 1; $x &lt; 50; ++$x) {
        echo similar_text (md5($x), md5($x*$x)), "&lt;br&gt;";
    }
}

// Display the data stored in the profiler
print_r (profile (TRUE));
?&gt;
&lt;/pre&gt;
     </programlisting>
    </example>
    The example profiles the PHP code within the 'declare'
    block, recording the time at which every second low-level
    statement in the block was executed. This information can
    then be used to find the slow areas within particular
    segments of code. This process can be performed using other
    methods: using ticks is more convenient and easier to
    implement.
   </para>
   <simpara>
    Ticks are well suited for debugging, implementing simple
    multitasking, backgrounded I/O and many other tasks.
   </simpara>
   <simpara>
    See also <function>register_tick_function</function> and
    <function>unregister_tick_function</function>.
   </simpara>
   </sect2>
  </sect1>
  
  <sect1 id="function.require">
   <title><function>require</function></title>
   <simpara>
    Het <function>require</function> statement vervangt zichzelf met
    de gespecificeerde file, bijna gelijk als de C preprocessor's
    <literal>#include</literal> werkt.
   </simpara>
   <simpara>
    Als &quot;URL fopen wrappers&quot; zijn enabled in PHP (dit zijn
    ze in de default distributie) dan kun je met
    <function>require</function> ook een URL gebruiken in plaats van
    een lokaal bestand. Zie <link linkend="features.remote-files">Remote
    files</link> en <function>fopen</function> voor meer informatie.
   </simpara>
   <simpara>
    Het is belangrijk om te weten dat wanneer er een bestand wordt
    ge<function>include</function>ed of ge<function>require</function>ed,
    de parser uit PHP komt en naar HTML mode gaat voordat het bestand
    wordt toegevoegd, en weer start met de PHP mode aan het einde van
    het bestand. Om deze reden moet alle code in het bestand dat als
    PHP code moet worden ge&iuml;nterpreteerd moet worden omvat
    met <link
    linkend="language.basic-syntax.phpmode">geldige PHP start en end
    tags</link>.
   </simpara>
   <simpara>
    <function>require</function> is eigenlijk geen functie in PHP;
    maar het is een taal constructie. Er gelden andere dingen
    voor constructies dan voor functies. Bijvoorbeeld is het
    niet mogelijk om <function>require</function> een return
    waarde mee te geven. Als je toch probeert een return waarde
    te lezen uit een <function>require</function> aanroep, krijg
    je een parse error.
   </simpara>
   <simpara>
    In tegenstelling tot <function>include</function> leest
    <function>require</function> <emphasis>altijd</emphasis>
    het bestand in, <emphasis>zelfs als de regel waarop de require
    call staat nooit wordt uitgevoerd.</emphasis>. Als je een
    bestand conditioneel wilt laden, gebruik dan de
    <function>include</function> functie. 
    Een conditioneel statement heeft geen invloed op een
    <function>require</function>. Als een regel waarop de
    <function>require</function> staat niet wordt uitgevoerd,
    dan zal de code in de required file ook niet worden uitgevoerd.
   </simpara>
   <simpara>
    Gelijksoortig hebben loop structuren geen effect op het gedrag van
    <function>require</function>. Hoewel de code in de required file
    nog steeds in de loop staat, wordt het
    <function>require</function> statement zelf maar &euml;&euml;n
    keer uitgevoerd.
   </simpara>
   <para>
    Al dit betekend dat je niet kunt verwachten dat
    <function>require</function> binnen een loop structuur per iteratie
    een ander besstand kan invoegen. Om zoiets te doen, kun je het
    <function>include</function> statement gebruiken.
    <informalexample>
     <programlisting role="php">
require ('header.inc');
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    Als een bestand is ge<function>require</function>ed, dan zal de
    code de inhoud van de variabele scope overnemen zoals deze was
    op het punt van de <function>require</function>. Alle variabelen
    die beschikbaar waren in de aanroepende file, zullen ook beschikbaar
    zijn de in aangeroepen file. Als de <function>require</function>
    optreed in een functie in de aanroepende file, dan zal de code
    die zich in het bestand dat ge-required wordt zich gedragen alsof
    deze in de functie waarin <function>require</function> wordt
    aangeroepen.
   </simpara>
   <para>
    Als de <function>require</function>ed file wordt opgeroepen over HTTP
    met gebruikmakend van de fopen wrappers, en als de target server dit
    bestand kan interpreteren als PHP code, dan mogen er variabelen 
    worden meegegeven aan de <function>require</function>ed file met
    gebruik maken als een HTTP GET regel (zie voorbeeld). Dit is niet
    precies hetzelfde als <function>require</function>ing een bestand
    en het laten overnemen van de variabele scope van de aanroepende
    file. In dit geval wordt het script uitgevoerd op de remote server
    en het resultaat van het script zla opgenomen worden in het lokale
    script.
    <informalexample>
     <programlisting role="php">
/* Dit voorbeeld gaat er van uit dat someserver is geconfigureerd om
 * .php bestanden uit te voeren, maar .txt file niet. Ook betekent
 * 'werkt' hier dat de variabelen $vareen en $vartwee beschikbaar
 * zijn binnen de require()ed file. */

/* Werkt niet; file.txt werd niet geparsed door someserver. */
require ("http://someserver/file.txt?varone=1&amp;vartwo=2");

/* Werkt niet; er wordt gekeken naar het bestand 'file.php?varone=1&amp;vartwo=2'
 * op het huidige bestandssysteem. */
require ("file.php?varone=1&amp;vartwo=2");               

/* Werkt. */
require ("http://someserver/file.php?varone=1&amp;vartwo=2"); 

$varone = 1;
$vartwo = 2;
require ("file.txt");  /* Werkt. */
require ("file.php");  /* Werkt. */
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    In PHP 3 is het mogelijk een <literal>return</literal> waarde terug
    te geven vanuit een <function>require</function>ed bestand, zo lang
    dat statement optreed binnen de globale scope van het 
    <function>require</function>ed bestand. Het mag niet optreden binnen
    een blok (d.w.z. binnen accolades ({}). In PHP 4, is deze mogelijkheid
    echter niet meer aanwezig. Als je dit toch wilt, zie dan de
    <function>include</function> functie.
   </simpara>
   <simpara>
    Zie ook <function>include</function>, <function>require_once</function>,
    <function>include_once</function>, <function>readfile</function>,
	en <function>virtual</function>.
   </simpara>
  </sect1>
 
  <sect1 id="function.include">
   <title><function>include</function></title>
   <simpara>
    Het <function>include</function> statement voegt het aangegeven
    bestand in en evalueert deze.
   </simpara>
   <simpara>
    Als de "URL fopen wrappers" zijn ingeschakeld (in de default configuratie
    is dit het gevel), dan kun je een file ook
    <function>include</function>n met een URL, in plaats van een lokale
    path naam. Zie <link linkend="features.remote-files">Remote
    files</link> en <function>fopen</function> voor meer informatie
    hierover.
   </simpara>
   <simpara>
    Een belangrijke opmerkinge plaatsen we hier over bestanden die
    worden ge-<function>include</function>ed of
    ge-<function>require</function>ed. Deze functies zorgen er beide
    voor dat de parser PHP mode verlaat en overgaat in HTML mode voor
    de opgegeven files aan het begin van het bestand, en aan het einde
    van dit bestand weer in PHP mode overgaat. Daarom moet de inhoud
    van deze bestanden worden omgeven met <link
    linkend="language.basic-syntax.phpmode">geldige PHP begin en einde
    tags</link>.
   </simpara>
   <para>
    Dit gebeurd iedere keer als <function>include</function> statement
    wordt tegegekomen, daarom kun je met een <function>include</function>
    in een loop structuur een aantal verschillende files invoegen.
    <informalexample>
     <programlisting role="php">
$files = array ('first.inc', 'second.inc', 'third.inc');
for ($i = 0; $i &lt; count($files); $i++) {
    include $files[$i];
}
     </programlisting>
    </informalexample>
   </para>
   <para>
    <function>include</function> verschilt van het 
    <function>require</function> statement op &eacute;&eacute; punt.
    Een <function>include</function> statement wordt elke keer
    ge-evalueerd als deze wordt tegengekomen (en ook alleen maar tijdens
    de uitvoering van een script). Dit in tegenstelling tot het
    <function>require</function> statement, welke wordt vervangen door
    de opgegeven file als deze voor de eerste keer wordt tegenkomen.
   </para>
   <para>
    Omdat <function>include</function> een speciale taal constructie
    is, ben je verplicht deze te omgeven met een statement blok als
    het <function>include</function> statement zich in een conditionele
    constructtie bevind.
    <informalexample>
     <programlisting role="php">
/* Dit is FOUT en zal niet werken zoals het bedoeld is */
 
if ($condition)
    include($file);
else
    include($other);
 
/* Dit is GOED. */
 
if ($condition) {
    include($file);
} else {
    include($other);
}
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    In zowerl PHP 3 als PHP 4, is het mogelijk om een
    <literal>return</literal> statement uit te voeren in een
    ge-<function>include</function>d file, om bijvoorbeeld een fout
    conditie terug tegeven aan het script wat het bestand uitvoerde.
    Er zijn wel verschillend in de manier waarop dit werkt. Het eerste
    verschil is dat in PHP 3 het <literal>return</literal> zich niet
    binnen een blok mag bevinden, tenzij het een functie blok betreft.
    In dit laatste geval heeft het <literal>return</literal> statement
    betrekking op de functie natuurlijk, en niet op de gehele file.
    In PHP 4 bestaat deze restrictie echte niet. Het is in PHP 4 ook
    mogelijk om waardes terug te geven van uit
    <function>include</function>d files. Je kunt dan de return waarde van
    het <function>include</function> statement gebruiken net zoals een
    gewone functie. In PHP 3 zal dit een parse error genereren.
   </simpara>
   <example>
    <title><function>include</function> in PHP 3 en PHP 4</title>
    <para>
     Naam aan dat het volgende bestand bestaat (genoemd
     <filename>test.inc</filename>) en dat deze zich in dezelfde
     directory bevind als het hoofd script:
     <programlisting role="php">
&lt;?php
echo "Voor de return &lt;br&gt;\n";
if (1) {
    return 27;
}
echo "Na de return &lt;br&gt;\n";
?&gt;
     </programlisting>
    </para>
    <para>
     Naam aan dat het hoofd script, (<filename>main.html</filename>)
     contains the following:
     <programlisting role="php">
&lt;?php
$retval = include ('test.inc');
echo "File returned: '$retval'&lt;br&gt;\n";
?&gt;
     </programlisting>
    </para>
    <para>
     When <filename>main.html</filename> is called in PHP 3, it will
     generate a parse error on line 2; you can't take the value of an
     <function>include</function> in PHP 3. In PHP 4, however, the
     result will be:
     <screen>
Before the return
File returned: '27'
     </screen>
    </para>
    <para>
     Now, assume that <filename>main.html</filename> has been altered
     to contain the following:
     <programlisting role="php">
&lt;?php
include ('test.inc');
echo "Back in main.html&lt;br&gt;\n";
?&gt;
     </programlisting>
    </para>
    <para>
     In PHP 4, the output will be:
     <screen>
Before the return
Back in main.html
     </screen>
     However, PHP 3 will give the following output:
     <screen>
Before the return 
27Back in main.html

Parse error: parse error in /home/torben/public_html/phptest/main.html on line 5
     </screen>
    </para>
    <para>
     The above parse error is a result of the fact that the
     <literal>return</literal> statement is enclosed in a non-function
     block within <filename>test.inc</filename>. When the return is
     moved outside of the block, the output is:
     <screen>
Before the return
27Back in main.html
     </screen>
    </para>
    <para>
     The spurious '27' is due to the fact that PHP 3 does not support
     <literal>return</literal>ing values from files like that.
    </para>
   </example>
   <simpara>
    When a file is <function>include</function>ed, the code it
    contains inherits the variable scope of the line on which the
    <function>include</function> occurs. Any variables available at
    that line in the calling file will be available within the called
    file. If the <function>include</function> occurs inside a
    function within the calling file, then all of the code contained
    in the called file will behave as though it had been defined
    inside that function.
   </simpara>
   <para>
    If the <function>include</function>ed file is called via HTTP
    using the fopen wrappers, and if the target server interprets the
    target file as PHP code, variables may be passed to the
    <function>include</function>ed file using an URL request string as
    used with HTTP GET. This is not strictly speaking the same thing
    as <function>include</function>ing the file and having it inherit
    the parent file's variable scope; the script is actually being run
    on the remote server and the result is then being included into
    the local script.
    <informalexample>
     <programlisting role="php">
/* This example assumes that someserver is configured to parse .php
 * files and not .txt files. Also, 'works' here means that the variables 
 * $varone and $vartwo are available within the include()ed file. */

/* Won't work; file.txt wasn't handled by someserver. */
include ("http://someserver/file.txt?varone=1&amp;vartwo=2");

/* Won't work; looks for a file named 'file.php?varone=1&amp;vartwo=2'
 * on the local filesystem. */
include ("file.php?varone=1&amp;vartwo=2");               

/* Works. */
include ("http://someserver/file.php?varone=1&amp;vartwo=2"); 

$varone = 1;
$vartwo = 2;
include ("file.txt");  /* Works. */
include ("file.php");  /* Works. */
     </programlisting>
    </informalexample>
   </para>
   <simpara>
    See also <function>require</function>, <function>require_once</function>,
    <function>include_once</function>, <function>readfile</function>,
	and <function>virtual</function>.
   </simpara>
  </sect1>
 
  <sect1 id="function.require-once">
   <title><function>require_once</function></title>
   <para>
    The <function>require_once</function> statement replaces
    itself with the specified file, much like the C preprocessor's
    <literal>#include</literal> works, and in that respect is
	similar to the <function>require</function> statement. The main
	difference is that in an inclusion chain, the use of
	<function>require_once</function> will assure that the code is
	added to your script only once, and avoid clashes with variable
	values or function names that can happen.
   </para>
   <para>
     For example, if you create the following 2 include files
	 <literal>utils.inc</literal> and <literal>foolib.inc</literal>
	 <example>
	 <title>utils.inc</title>
	 <programlisting role="php">
&lt;?php
define(PHPVERSION, floor(phpversion()));
echo "GLOBALS ARE NICE\n";
function goodTea() {
	return "Oolong tea tastes good!";
}
?&gt;
	 </programlisting>
	 </example>
	 <example>
	 <title>foolib.inc</title>
	 <programlisting role="php">
&lt;?php
require ("utils.inc");
function showVar($var) {
	if (PHPVERSION == 4) {
		print_r($var);
	} else {
		var_dump($var);
	}
}

// bunch of other functions ...
?&gt;
	 </programlisting>
	 </example>
	 And then you write a script <literal>cause_error_require.php</literal>
	 <example>
	 <title>cause_error_require.php</title>
	 <programlisting role="php">
&lt;?php
require("foolib.inc");
/* the following will generate an error */
require("utils.inc");
$foo = array("1",array("complex","quaternion"));
echo "this is requiring utils.inc again which is also\n";
echo "required in foolib.inc\n";
echo "Running goodTea: ".goodTea()."\n";
echo "Printing foo: \n";
showVar($foo);
?&gt;
	 </programlisting>
	 </example>
	 When you try running the latter one, the resulting ouptut will be (using
	 PHP 4.01pl2):
	 <informalexample>
	 <programlisting>
GLOBALS ARE NICE
GLOBALS ARE NICE

Fatal error:  Cannot redeclare causeerror() in utils.inc on line 5
	 </programlisting>
	 </informalexample>
	 By modifying <literal>foolib.inc</literal> and
	 <literal>cause_errror_require.php</literal> 
	 to use <function>require_once</function>
	 instead of <function>require</function> and renaming the
	 last one to <literal>avoid_error_require_once.php</literal>, we have:
	 <example>
	 <title>foolib.inc (fixed)</title>
	 <programlisting role="php">
...
require_once("utils.inc");
function showVar($var) {
...
	 </programlisting>
	 </example>
	 <example>
	 <title>avoid_error_require_once.php</title>
	 <programlisting role="php">
...
require_once("foolib.inc");
require_once("utils.inc");
$foo = array("1",array("complex","quaternion"));
...
	 </programlisting>
	 </example>
	 And when running the latter, the output will be (using PHP 4.0.1pl2):
	 <informalexample>
	 <programlisting>
GLOBALS ARE NICE
this is requiring globals.inc again which is also
required in foolib.inc
Running goodTea: Oolong tea tastes good!
Printing foo:
Array
(
    [0] =&gt; 1
    [1] =&gt; Array
        (
            [0] =&gt; complex
            [1] =&gt; quaternion
        )

)
	 </programlisting>
	 </informalexample>
   </para>
   <para>
     Also note that, analogous to the behavior of the
	 <literal>#include</literal> of the C preprocessor, this statement
	 acts at "compile time", e.g. when the script is parsed and before it
	 is executed, and should not be used for parts of the script that need
	 to be inserted dynamically during its execution. You should use
	 <function>include_once</function> or <function>include</function>
	 for that purpose.
   </para>
   <para>
     For more examples on using <function>require_once</function> and 
	 <function>include_once</function>, look at the PEAR code included in
	 the latest PHP source code distributions.
   </para>
   <para>
    See also: <function>require</function>,
    <function>include</function>, <function>include_once</function>,
    <function>get_required_files</function>,
    <function>get_included_files</function>, <function>readfile</function>,
	and <function>virtual</function>.
   </para>
  </sect1>
  
  <sect1 id="function.include-once">
   <title><function>include_once</function></title>
   <para>
    The <function>include_once</function> statement includes and evaluates
    the specified file during the execution of the script.
	This is a behavior similar to the <function>include</function> statement,
	with the important difference that if the code from a file has already
	been included, it will not be included again.
   </para>
   <para>
    As mentioned in the <function>require_once</function> description, the
	<function>include_once</function> should be used in the cases in which
	the same file might be included and evaluated more than once during a
	particular execution of a script, and you want to be sure that it is
	included exactly once to avoid problems with function redefinitions,
	variable value reassignments, etc.
   </para>
   <para>
     For more examples on using <function>require_once</function> and 
	 <function>include_once</function>, look at the PEAR code included in
	 the latest PHP source code distributions.
   </para>
   <para>
     <function>include_once</function> was added in PHP 4.0.1pl2
   </para>
   <para>
    See also: <function>require</function>,
    <function>include</function>, <function>require_once</function>,
    <function>get_required_files</function>,
    <function>get_included_files</function>, <function>readfile</function>,
	and <function>virtual</function>.
   </para>
  </sect1>
 
 </chapter>
 
 <!-- Keep this comment at the end of the file
 Local variables:
 mode: sgml
 sgml-omittag:t
 sgml-shorttag:t
 sgml-minimize-attributes:nil
 sgml-always-quote-attributes:t
 sgml-indent-step:1
 sgml-indent-data:t
indent-tabs-mode:nil
 sgml-parent-document:nil
 sgml-default-dtd-file:"../../manual.ced"
 sgml-exposed-tags:nil
 sgml-local-catalogs:nil
 sgml-local-ecat-files:nil
 End:
 -->