PHP: 関数 - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<include_once関数の引数>
view the version of this page
Last updated: Tue, 21 Dec 2004

第 17章関数

ユーザー定義関数

関数は次のような構文で定義されます。

<?php
function foo ($arg_1, $arg_2, ..., $arg_n)
{
   echo
"関数の例\n";
   return
$retval;
}
?>

関数の中では、他の関数や クラス 定義 を含む PHP のあらゆる有効なコードを使用することができます。

PHP 3 では、関数は参照される前に定義されている必要がありました。 PHP 4ではそのような制限はありません。 ただし以下の二つの例のように、条件付きで関数が 定義されるような場合を除きます

次の二つの例のように、ある条件下でのみ関数が定義される場合には、 その関数定義は関数がコールされる前に 行われていなければなりません。

例 17-1. 条件つきの関数

<?php

$makefoo
= true;

/* ここでは関数foo()はまだ定義されていないので
   コールすることはできません。
   しかし関数 bar()はコールできます。 */

bar();

if (
$makefoo) {
  function
foo ()
  {
   echo
"I don't exist until program execution reaches me.\n";
  }
}

/* ここでは $makefooがtrueと評価されているため
   安全にfoo()をコールすることができます。 */

if ($makefoo) foo();

function
bar()
{
  echo
"I exist immediately upon program start.\n";
}

?>

例 17-2. 関数の中の関数

<?php
function foo()
{
  function
bar()
  {
   echo
"I don't exist until foo() is called.\n";
  }
}

/* ここでは関数bar()はまだ定義されていないので
   コールすることはできません。 */

foo();

/* foo()の実行によって bar()が
   定義されるためここではbar()を
   コールすることができます。*/

bar();

?>

PHP は関数のオーバーロードをサポートしていません。 また、宣言された関数の定義を取り消したり再定義することも できません。

注意: 関数名は大文字小文字を区別しませんが、通常は 関数宣言時と同じ名前で関数をコールする方が好ましいです。

PHP 3 では、引数のデフォルト値(詳細は、引数のデフォルト値を 参照下さい)はサポートしていますが、関数の引数を可変とすることはで きません。PHP 4 は両方ともサポートしています。詳細は、 可変長引数リスト および func_num_args(), func_get_arg(), func_get_args() に関する関数リファレンスを 参照下さい。



add a note add a note User Contributed Notes
関数
ardk at vfemail dot net
12-Mar-2005 10:33
<?php
/* Place in body of page */
function NotExist($pageID)
{
   return
$pageID != 1 && $pageID != 2;
}
if (
$_GET['id'] == 1) {
   echo
"<br /><br />PageID 1<br />";
}
if (
$_GET['id'] == 2) {
   echo
"<br /><br />PageID 2<br />";
}
if (
NotExist($_GET['id'])) {
   echo
"<br /><br />Wrong PageID!<br />";
}
?>
roy at intelligentdashimaging dot com
07-Feb-2005 09:17
Here's how to get static function behavior and instantiated object behavior in the same function
<?php

// Test of static/non-static method overloading, sort of, for php
class test {
   var
$Id = 2;
   function
priint ($id = 0) {
       if (
$id == 0) {
           echo
$this->Id;
           return;
       }
       echo
$id;
       return;
   }
}
$tc = new test ();
$tc->priint ();
echo
"\n";
test::priint (1);
/* output:
2
1
*/
?>
30-Jan-2005 01:08
good<?php
function foo()
{
  function
bar()
  {
   echo
"I don't exist until foo() is called.\n";
  }
}

/* We can't call bar() yet
   since it doesn't exist. */

foo();

/* Now we can call bar(),
   foo()'s processesing has
   made it accessible. */

bar();

?>
spy-j at rainbowtroopers dot com
21-Dec-2004 11:01
Take care when using parameters passed by reference to return new values in it:

<?PHP

function foo( &$refArray, ....) {
 
  UNSET(
$refArray); //<-- fatal!!!

 
for (....) {
  
$refArray[] = ....
  }
//end for

}//end foo

?>

I tried to make sure the passed variable is empty so i can fill it up as an array. So i put the UNSET. Unfortunately, this seems to cause a loss with the reference to the passed variable: the filled array was NOT returned in the passed argument refArray!

I have found that replacing unset as follows seems to work correctly:

<?PHP

function bar( &$refArray, ....) {
  
  if (!EMPTY(
$refArray)) $refArray = '';

  : : :

}
//end bar

?>
chris at infinitycubed dot net
11-Aug-2004 01:39
Remember, theres an additional overhead when calling functions, so if performance is key to what you are doing, you want to avoid calling out to other functions.

In a function I was making that found out the distance and angle from 1 point to another, there were 3 calls to a small validation function. It averaged ~0.017 seconds for 400 calls to the function, and with these calls replaced with the actual code, this lowered to 0.011-0.012. So, if you need performance, avoid using other functions.
jose at rondamagazine dot com
17-Jul-2004 01:34
A PHP4 -> PHP5 migration issue:

In PHP5, you can't declare a function inside a class method and call it from inside the method. An example:

<?php
class A {
   function
f() {
     function
inside_f() {
         echo
"I'm inside_f";
     }
     echo
"I'm f";
    
inside_f();
   }
}
?>

PHP5 reports an "Fatal error:  Non-static method A::inside_f() cannot be called statically ..."

Solution: Convert inside_f() to a class method, so you can call it from f() as $this->inside_f().
This will work in PHP4 and PHP5.
And, if you don't mind PHP4 compatibility, you should probably declare inside_f() as a private method, because it is declared inside f() for its private use (at least, that's what I believed I was doing; only now have I discovered that the original inside_f(), as declared, is a global function).
removeloop at removesuperinfinite dot com
03-Aug-2003 08:56
Method overloading is however permitted.

<?php
class A {
       function
A() { }

       function
ech() {
              
$a = func_get_args();
               for(
$t=0;$t<count($a); $t++ ) {
                       echo
$a[$t];
               }
       }
}       

$test = new A();
$test->ech(0,1,2,3,4,5,6,7,8,9);

?>

// output:
// 0123456789
danvk at rice dot edu
09-Jun-2003 06:04
While PHP does allow you to define a function within another function, this feature doesn't work in at all the same way that it does in most other languages. The most common use of an inner subroutine that I've seen is to define a helper function which can't be seen from outside, and still has access to the outer subroutine's local variables. Like this:

function a(){
  function b(){
   print $localVariable;
  }
  $localVariable = 10;
  b();
}

In PHP, this will cause all sorts of problems. Calling a() won't print anything, since $localVariable hasn't been defined within b(), even though it is defined within a(). And calling a() twice will result in an error that you're trying to redefine b().
kop at meme dot com
08-Jun-2003 10:08
You can preface a (builtin php ?) function call with an @ sign, as in:

@foo();

This will supress the injection of error messages into the data stream output to the web client.  You might do this, for example, to supress the display of error messages were foo() a database function and the database server was down.  However, you're probably better off using php configuration directives or error handling functions than using this feature.

See the section on error handling functions.
Storm
10-May-2003 02:55
I think it worthy of noting (for noobies such as myself):
You can define access to a global variable before it is defined when defining a function as long as the variable is defined before the function is called. This had me baffled for a few hours til I tried it out...lol. Here's a quick example:

Perfectly valid:
-------------------------
function hello() {
   global $hi;

   echo $hi;
}

$hi = 'Hi There!'; // Var defined after function is defined

hello();
-------------------------

I know most of the Gurus persay already knew this, but I didn't! :p This helps ;-)
php at simoneast dot net
11-Apr-2003 04:59
If you're frustrated by not having access to global variables from within your functions, instead of declaring each one (particularly if you don't know them all) there are a couple of workarounds...

If your function just needs to read global variables...

function aCoolFunction () {
   extract($GLOBALS);
....

This creates a copy of all the global variables in the function scope.  Notice that because it's a copy of the variables, changing them won't affect the variables outside the function and the copies are lost at the conclusion of the function.

If you need to write to your global variables, I haven't tested it, but you could probably loop through the $GLOBALS array and make a "global" declaration for each one.  Then you could modify the variables.

Please note that this shouldn't be standard practice, but only in the case where a function needs access to all the global variables when they may be different from one call to another.  Use the "global var1, var2..." declaration where possible.

Hope that helps some people.

Simon.
nutbar at innocent dot com
12-Mar-2003 08:06
Regarding the comments about having to declare global variables inside of functions before you can use them...

Lots of you seem to complain about having to declare lots of variables, when really there's one simple solution to this:

global $GLOBALS;

This will define the $GLOBALS variable inside your code, and since that variable is basically like the mother of all variables - *presto*, you now have access to any variable in PHP.
mittag /// add /// marcmittag /// de
23-Jan-2003 12:31
To devciti at yahoo dot com

The section "returning values" of the docu says:

=====
You can't return multiple values from a function, but similar results can be obtained by returning a list.

function small_numbers()
{
   return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
=====
arathorn at ifrance dot com
01-Jan-2003 03:02
If u want to put somes variables in function that was'nt passed by it, you must use "global" :

<?php

$op2
= blabla;
$op3 = blabla;

function
foo($op1)
{
  global
$op2, $op3;

   echo
$op1;
   echo
$op2;
   echo
$op3;
}

?>
misc dot anders at feder dot dk
24-Dec-2002 11:28
PHP allows you to address functions in a very dynamic way:

$foo = "bar";
$foo("fubar");

The above will call the bar function with the "fubar" argument.
albaity at php4web dot com
26-Oct-2002 02:06
To use class from function
you need first to load class OUT the function
and then you can use the class functions from your function
example :
class Cart
{
   var $items;  // Items in our shopping cart
  
   // Add $num articles of $artnr to the cart
 
   function add_item ($artnr, $num)
   {
       $this->items[$artnr] += $num;
   }
  
   // Take $num articles of $artnr out of the cart
 
   function remove_item ($artnr, $num)
   {
       if ($this->items[$artnr] > $num) {
           $this->items[$artnr] -= $num;
           return true;
       } else {
           return false;
       } 
   }
}

------------------------
<?php
$cart
= new Cart;

function
additem($var1,$var2){
$cart->add_item($var1, $var2);
}
additem(1,10);
?>
06-Sep-2002 08:41
If you need to use "real" global variables, accessible from any function, without the need to declare them explicity, take a look at (join these 2 lines please):

s=1ac53d13a815427f145d5dedf6a10023&threadid=10209242
matt at smidwap dot com
14-Aug-2002 12: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.
germanAlonso at keltoi-web dot com
10-Aug-2002 02: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).
bishop
01-May-2002 02: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';
}
bishop
01-May-2002 02: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
fabio at city dot ac dot uk
14-Feb-2002 02: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.
xpaz at somm dot com
14-Nov-2001 08: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.
aboyd at ssti dot com
05-Apr-2001 03: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.
yasuo_ohgaki at hotmail dot com
09-Mar-2001 06: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('<br>Recursion TEST<br>');

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 ==
21-Dec-2000 06: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..
kop at meme dot com
14-Dec-2000 11: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".
php-general at list dot php dot net
06-Dec-2000 12: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."
GMCardoe at netherworldrpg dot net
24-Apr-2000 09: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.
cap at capsi dot cx
22-Feb-2000 12: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.
php at paintbot dot com
05-Feb-2000 03: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]
brooke at wayport dot net
08-Nov-1999 08: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.)

<include_once関数の引数>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page