PHP: F�ggv�nyek - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousinclude_onceF�ggv�nyargumentumoknext
Last updated: Fri, 30 Aug 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | German | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

Fejezet 13. F�ggv�nyek

Felhaszn�l� �ltal defini�lt f�ggv�nyek

F�ggv�nyeket a k�vetkez� szintaxis szerint defini�lhatod:

function foo ($arg_1, $arg_2, ..., $arg_n)
{
    echo "P�lda f�ggv�ny.\n";
    return $retval;
}

B�rmely �rv�nyes PHP k�d megjelenhet egy f�ggv�nyen bel�l, ak�r m�g m�s f�ggv�ny vagy oszt�ly defin�ci�k is.

PHP 3-ban a f�ggv�nyeket defini�lni kell, miel�tt hivatkoz�s t�rt�nik r�juk (f�ggv�nyh�v�s el�tt). PHP 4-ben nincs ez a megk�t�s.

A PHP nem t�mogatja a f�ggv�nyek polimorfizmus�t (t�bbalak�s�g�t), a f�ggv�nyekdefin�ci�kat nem lehet megsz�ntetni vagy �jradefini�lni egy m�r defini�lt f�ggv�nyeket.

A PHP 3 nem t�mogatja a v�ltoz� sz�m� f�ggv�nyargumentumokat, b�r az argumentumok kezd��rt�ke t�mogatott. L�sd az Argumentumok kezd��rt�ke c�m� r�szt b�vebb inform�ci��rt. A PHP 4 mindkett� lehet�s�get t�mogatja. L�sd a V�ltoz� sz�m� f�ggv�nyargumentumok c�m� r�szt �s a func_num_args(), func_get_arg() �s a func_get_args() f�ggv�nyeket r�szletesebb le�r�s�rt.

User Contributed Notes
F�ggv�nyek
add a note about notes
[email protected]
08-Nov-1999 06:21

Many people ask how to call functions that are in other files. See "Require" and "Include" in the manual. Also the value of $DOCUMENT_ROOT is good for including sub-includes. (It is NOT like C/C++ includes. The dir is ALWAYS relative to the main source file.)
[email protected]
05-Feb-2000 01:32

Important Note to All New Users: functions do NOT have default access to GLOBAL variables. You must specify globals as such in your function using the 'global' type/keyword. See the section on variables:scope.

This note should also be added to the documentation, as it would help the majority of programmers who use languages where globals are, well, global (that is, available from anywhere). The scoping rules should also not be buried in subsection 4 of the variables section. It should be front and center because I think this is probably one of the most non-standard and thus confusing design choices of PHP.

[Ed. note: the variables $_GET, $_POST, $_REQUEST, $_SESSION, and $_FILES are superglobals, which means you don't need the global keyword to use them inside a function]

[email protected]
22-Feb-2000 10:19

When using a function within a function, using global in the inner function will not make variables available that have been first initialized within the outer function.
[email protected]
24-Apr-2000 08:02

Stack overflow means your function called itself recursivly too many times and just completely filled up the processes stack. That error is there to stop a recursive call from completely taking up the entire system memory.
[email protected]
05-Dec-2000 10:51

If a user-defined function does not explicitly return a value, then it
will return NULL. For most truth tests, this can be considered as FALSE.

For example:

function print_list ($array)
{
print implode ("<br />", $array);
}

if (print_list ($HTTP_POST_VARS))
print "The print_list function returned a value that can be considered true."
else
print "The print_list function returned a value that can be considered false."

[email protected]
14-Dec-2000 09:14

See also about controlling the generation of error messages by putting @ in front of the function before you call it, in the section "error control operators".
21-Dec-2000 04:41
Function names are case-insensitive in PHP 3 and PHP 4.

For example, if you have function foo, you can call it using foo(), FoO(), FOO(), etc..

[email protected]
09-Mar-2001 04:42

PHP supports recursion. I thought it worth to mention.

Simple Quick Sort using recursion works perfectly.

== OUTPUT ==
Recursion TEST

Array
(
[0] => 12
[1] => 23
[2] => 35
[3] => 45
[4] => 56
[5] => 67
)
== END OUTPUT ==

== QUICK SORT CODE ==
<?php

echo('
Recursion TEST
');

function swap(&$v, $i, $j) {
$temp = $v[$i];
$v[$i] = $v[$j];
$v[$j] = $temp;
}

// Quick Sort integer array - $int_array[$left] .. $int_array[$right]
function qsort(&$int_array, $left, $right) {
if ($left >= $right)
return; // Do nothing if there are less than 2 array elements
swap ($int_array, $left, intval(($left+$right)/2));
$last = $left;
for ($i = $left + 1; $i <= $right; $i++)
if ($int_array[$i] < $int_array[$left])
swap($int_array, ++$last, $i);
swap($int_array, $left, $last);
qsort($int_array, $left, $last-1);
qsort($int_array, $last+1, $right);
}

$val = array(56,23,45,67,12,35);

qsort($val, 0, count($val)-1);

echo '<pre>';
print_r ($val);
echo '</pre>';

?>
== END QUICK SORT ==

[email protected]
05-Apr-2001 02:34

[Editor's note: put your includes in the beginning of your script. You can call an included function, after it has been included --jeroen]

The documentation states: "In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4."

I thought it wise to note here that there is in fact a limitation: you cannot bury your function in an include() or require(). If the function is in an include()'d file, there is NO way to call that function beforehand. The workaround is to put the function directly in the file that calls the function.

[email protected]
14-Nov-2001 06:47

It is possible to define a function from inside another function.
The result is that the inner function does not exist until the outer function gets executed.

For example, the following code:

function a () {
function b() {
echo "I am b.\n";
}
echo "I am a.\n";
}
if (function_exists("b")) echo "b is defined.\n"; else echo "b is not defined.\n";
a();
if (function_exists("b")) echo "b is defined.\n"; else echo "b is not defined.\n";

echoes:

b is not defined.
I am a.
b is defined.

Classes too can be defined inside functions, and will not exist until the outer function gets executed.

[email protected]
14-Feb-2002 12:57

As a corollary to other people's contributions in this section, you have to be careful when transforming a piece of code in to a function (say F1). If this piece of code contains calls to another function (say F2), then each variable used in F2 and defined in F1 must be declared as GLOBAL both in F1 and F2. This is tricky.
bishop
01-May-2002 01:49

The documentation statement:

"In PHP 3, functions must be defined before they are referenced. No such requirement exists in PHP 4."

is not entirely accurate (or at least misleading).

Consider:
function a() {
function b() {
echo 'I am b';
}
echo 'I am a';
}

You MUST call a() before you can even think about using b(). Why? The parser hasn't touched the scope inside function a (for efficiency reasons), so b has yet to be defined or even *declared*.

The documentation (I believe) refers to this behaviour:
a();

function a() {
echo 'I am a';
}

which is perfectly valid and runs as you probably expect. However, the following does not work as expected (or documented):

function a() {
b();

function b() {
echo 'I am b';
}
echo 'I am a';
}

a();

Rather than getting "I am b" followed by "I am a" you get a parse error ("Call to undefined function b()").

So, the bottom line: Gewaehrleistungsausschluss

bishop
01-May-2002 01:54

Consider:

function a() {
function b() {
echo 'I am b';
}
echo 'I am a';
}

a();
a();

As you might NOT expect, the second call to a() fails with a "Cannot redeclare b()" error. This behaviour is correct, insofar as PHP doesn't "allow functions to be redefined."

A work around:
function a() {
if ( ! function_exists('b') ) {
function b() {
echo 'I am b';
}
}
echo 'I am a';
}

[email protected]
20-Jun-2002 03:51

I have some problems with functions. I want to create some kind of 'tell me the configuration function'. Like this:
<?php
function TellConfig(){
$header_file_to_use = "header.inc";
$footer_file_to_user = "footer.inc";
}
//Wont Work!
TellConfig();
include($header_file_to_use);
echo "Body!";
include($footer_file_to_use);
/*
Is there a way to call a variable set by a function? As i dont know this is the way i am doing it
*/
$header_file_to_use = "";
$footer_file_to_use = "";
function TellConfig2(){
global $header_file_to_use, $footer_file_to_use;
$header_file_to_use = "header.inc";
$footer_file_to_use = "footer.inc";
}
TellConfig2();
include($header_file_to_use);
echo "body 2!";
include($footer_file_to_use);
// This works
?>

Even i dont want to use a function for configuration vars this is a good example.
:P

[email protected]
10-Aug-2002 01:17

Although [email protected] has already pointed the recursion support on PHP, here's another example, wich shows clearly the mechanism of recursive algorithms:

function fact($i){
if($i==1){
return 1;
}else{
return $i*fact($i-1);
}
}

It returns $i! (supposing $i is a valid positive integer greater than 0).

[email protected]
13-Aug-2002 11:26

[Editor's note: that is the reason why 'function' and others are "reserved words", see: for more details]

Creating a function with the name 'function' won't turn up errors, but when calling that function, nothing will be returned. Example:

<?php
function();
function function () {
print "Hello, world!";
}
// "Hello, world!" will not be printed.
?>

I assume this is because when calling 'function', php thinks you are trying to create a new function.

add a note about notes
previousinclude_onceF�ggv�nyargumentumoknext
Last updated: Fri, 30 Aug 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Aug 31 06:19:44 2002 CEST