File: functions.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 (359 lines) | stat: -rw-r--r-- 10,257 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
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- up-to-date against phpdoc/en/language/functions.xml:1.9 -->
 <chapter id="functions">
  <title>Functies</title>

  <sect1 id="functions.user-defined">
   <title>Door de gebruiker gedefineerde functies</title>
 
   <para>
    Een functie kan gedefineerd worden met een syntax die luid als
    volgt:
 
    <informalexample>
     <programlisting role="php">
function foo ($arg_1, $arg_2, ..., $arg_n) {
    echo "Voorbeeld functie.\n";
    return $retval;
}
     </programlisting>
    </informalexample>
   </para>
     
   <simpara>
    Elke geldige PHP code mag binnen een functie gebruikt worden, zelfs
    andere cunties en <link linkend="keyword.class">class</link>
    definities.
   </simpara>
   <simpara>
    In PHP 3 moeten functies gedefineerd worden voordat ze worden gebruikt.
    Deze eis bestaat niet meer in PHP 4.
   </simpara>
   <simpara>
    PHP heeft geen ondersteuning voor functie overloading en het is ook
    niet mogelijk eerder gedefineerde te un-defineren.
   </simpara>
   <simpara>
    PHP 3 ondersteund niet het gebruik van een variabel aantal parameters
    voor functies, maar wel het gebruik van default parameters (zie <link
    linkend="functions.arguments.default">Default parameters
    </link> voor meer informatie). PHP 4 ondersteund beide, zie voor meer
    informatie: <link
    linkend="functions.variable-arg-list">Parameterlijsten met een variabele
    grootte</link> en de documentatie van de volgende functies:
    <function>func_num_args</function>,
    <function>func_get_arg</function>, en
    <function>func_get_args</function>.
   </simpara>

  </sect1>
 
  <sect1 id="functions.arguments">
   <title>Functie parameters</title>
 
   <simpara>
    Gegevens kunnen aan functies worden doorgegeven doormiddel van een
    parameter lijst, dat een komma-gescheiden lijst is van variabelen en/of
    constanten.
   </simpara> 
   <para>
     PHP ondersteund het doorgeven van parameters &quot;by value&quot;
     (de standaard), <link
     linkend="functions.arguments.by-reference">het doorgeven met een
     referentie</link>, en <link
     linkend="functions.arguments.default">default parameter waarden</link>
     Parameter lijsten met variabele lengtes worden alleen maar ondersteund
     in PHP 4 en later; zie <link
     linkend="functions.variable-arg-list">Parameterlijsten met variabele
     grootte</link> en de documentatie van de volgende functies:
     <function>func_num_args</function>,
     <function>func_get_arg</function>, en
     <function>func_get_args</function>. Het kan wel gesimuleerd worden in
     PHP 3 door een array mee te geven als parameter aan de functie:
 
    <informalexample>
     <programlisting role="php">
function takes_array($input) {
    echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
     </programlisting>
    </informalexample>
   </para>
 
   <sect2 id="functions.arguments.by-reference">
    <title>Making arguments be passed by reference</title>
 
    <simpara>
     By default, function arguments are passed by value (so that if
     you change the value of the argument within the function, it does
     not get changed outside of the function). If you wish to allow a
     function to modify its arguments, you must pass them by
     reference.
    </simpara>
    <para>
     If you want an argument to a function to always be passed by
     reference, you can prepend an ampersand (&amp;) to the argument
     name in the function definition:
 
     <informalexample>
      <programlisting role="php">
function add_some_extra(&amp;$string) {
    $string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str;    // outputs 'This is a string, and something extra.'
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     If you wish to pass a variable by reference to a function which
     does not do this by default, you may prepend an ampersand to the
     argument name in the function call:
 
     <informalexample>
      <programlisting role="php">
function foo ($bar) {
    $bar .= ' and something extra.';
}
$str = 'This is a string, ';
foo ($str);
echo $str;    // outputs 'This is a string, '
foo (&amp;$str);
echo $str;    // outputs 'This is a string, and something extra.'
      </programlisting>
     </informalexample>
    </para>

   </sect2>
 
   <sect2 id="functions.arguments.default">
    <title>Default argument values</title>
 
    <para>
     A function may define C++-style default values for scalar
     arguments as follows:
 
     <informalexample>
      <programlisting role="php">
function makecoffee ($type = "cappucino") {
    return "Making a cup of $type.\n";
}
echo makecoffee ();
echo makecoffee ("espresso");
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     The output from the above snippet is:
 
     <screen>
Making a cup of cappucino.
Making a cup of espresso.
     </screen>
    </para>
 
    <simpara>
     The default value must be a constant expression, not (for
     example) a variable or class member.
    </simpara>
    <para>
     Note that when using default arguments, any defaults should be on
     the right side of any non-default arguments; otherwise, things
     will not work as expected. Consider the following code snippet:
 
     <informalexample>
      <programlisting role="php">
function makeyogurt ($type = "acidophilus", $flavour) {
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt ("raspberry");   // won't work as expected
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     The output of the above example is:
 
     <screen>
Warning: Missing argument 2 in call to makeyogurt() in 
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .
     </screen>
    </para>
 
    <para>
     Now, compare the above with this:
 
     <informalexample>
      <programlisting role="php">
function makeyogurt ($flavour, $type = "acidophilus") {
    return "Making a bowl of $type $flavour.\n";
}
 
echo makeyogurt ("raspberry");   // works as expected
      </programlisting>
     </informalexample>
    </para>
 
    <para>
     The output of this example is:
 
     <screen>
Making a bowl of acidophilus raspberry.
     </screen>
    </para>

   </sect2>

   <sect2 id="functions.variable-arg-list">
    <title>Variable-length argument lists</title>
	
    <simpara>
     PHP 4 has support for variable-length argument lists in
     user-defined functions. This is really quite easy, using the
     <function>func_num_args</function>,
     <function>func_get_arg</function>, and
     <function>func_get_args</function> functions.
    </simpara>

    <simpara>
     No special syntax is required, and argument lists may still be
     explicitly provided with function definitions and will behave as
     normal.
    </simpara>

   </sect2>

  </sect1>
 
  <sect1 id="functions.returning-values">
   <title>Returning values</title>
 
   <para>
    Values are returned by using the optional return statement. Any
    type may be returned, including lists and objects.
 
    <informalexample>
     <programlisting role="php">
function square ($num) {
    return $num * $num;
}
echo square (4);   // outputs '16'.
     </programlisting>
    </informalexample>
   </para>
      
   <para>
    You can't return multiple values from a function, but similar
    results can be obtained by returning a list.
 
    <informalexample>
     <programlisting role="php">
function small_numbers() {
    return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
     </programlisting>
    </informalexample>
   </para>
   <para>
    To return a reference from a function, you have to use
    the reference operator &amp; in both the function declaration and
    when assigning the returned value to a variable:
    <informalexample>
     <programlisting role="php">
function &amp;returns_reference() {
    return $someref;
}

$newref =&amp;returns_reference();
     </programlisting>
    </informalexample>
   </para>

  </sect1>
 
  <sect1 id="functions.old-syntax">
   <title><literal>old_function</literal></title>
 
   <simpara>
    The <literal>old_function</literal> statement allows you to
    declare a function using a syntax identical to PHP/FI2 (except you
    must replace 'function' with 'old_function'.
   </simpara>
   <simpara>
    This is a deprecated feature, and should only be used by the
    PHP/FI2->PHP 3 convertor.
   </simpara>
   <warning>
    <para>
     Functions declared as <literal>old_function</literal> cannot be
     called from PHP's internal code. Among other things, this means
     you can't use them in functions such as
     <function>usort</function>, <function>array_walk</function>, and
     <function>register_shutdown_function</function>. You can get
     around this limitation by writing a wrapper function (in normal
     PHP 3 form) to call the <literal>old_function</literal>.
    </para>
   </warning>

  </sect1>

  <sect1 id="functions.variable-functions">
   <title>Variable functions</title>

   <para>
    PHP supports the concept of variable functions. This means that if
    a variable name has parentheses appended to it, PHP will look for
    a function with the same name as whatever the variable evaluates
    to, and will attempt to execute it. Among other things, this can
    be used to implement callbacks, function tables, and so forth.
   </para>

   <para>
    <example>
     <title>Variable function example</title>
     <programlisting role="php">
&lt;?php
function foo() {
    echo "In foo()&lt;br>\n";
}

function bar( $arg = '' ) {
    echo "In bar(); argument was '$arg'.&lt;br>\n";
}

$func = 'foo';
$func();
$func = 'bar';
$func( 'test' );
?>
     </programlisting>
    </example>
   </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:
 -->