PHP 5.6.18 is available

用户自定义函数

一个函数可由以下的语法来定义:

Example #1 展示函数用途的伪代码

<?php
function foo($arg_1$arg_2/* ..., */ $arg_n)
{
    echo 
"Example function.\n";
    return 
$retval;
}
?>

任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和定义。

函数名和 PHP 中的其它标识符命名规则相同。有效的函数名以字母或下划线打头,后面跟字母,数字或下划线。可以用正则表达式表示为:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

函数无需在调用之前被定义,除非是下面两个例子中函数是有条件被定义时。

当一个函数是有条件被定义时,必须在调用函数之前定义。

Example #2 有条件的函数

<?php

$makefoo 
true;

/* 不能在此处调用foo()函数,
   因为它还不存在,但可以调用bar()函数。*/

bar();

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

/* 现在可以安全调用函数 foo()了,
   因为 $makefoo 值为真 */

if ($makefoofoo();

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

?>

Example #3 函数中的函数

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

/* 现在还不能调用bar()函数,因为它还不存在 */

foo();

/* 现在可以调用bar()函数了,因为foo()函数
   的执行使得bar()函数变为已定义的函数 */

bar();

?>

PHP 中的所有函数和类都具有全局作用域,可以定义在一个函数之内而在之外调用,反之亦然。

PHP 不支持函数重载,也不可能取消定义或者重定义已声明的函数。

Note: 函数名是大小写无关的,不过在调用函数的时候,使用其在定义时相同的形式是个好习惯。

PHP 的函数支持可变数量的参数默认参数。参见 func_num_args()func_get_arg()func_get_args()

在 PHP 中可以调用递归函数。

Example #4 递归函数

<?php
function recursion($a)
{
    if (
$a 20) {
        echo 
"$a\n";
        
recursion($a 1);
    }
}
?>

Note: 但是要避免递归函数/方法调用超过 100-200 层,因为可能会使堆栈崩溃从而使当前脚本终止。 无限递归可视为编程错误。

add a note add a note

User Contributed Notes 6 notes

up
3
Muneeb Aslam
2 months ago
following is a function that can be used to convert numeric date to alphabetic date, e-g from 2015-11-16 to 16 Nov, 2015.

1. Function takes 3 parameters, numeric date, locale and length of month
2. Function currently supports EN and ES month names.
3. Function can be calles as <?php convertDate("2015-11-16","en","full"); ?>

<?php

   
function convertDate($date,$locale,$length){
       
       
$monthNames = array(
               
"en" => array(
                   
"full" => array(1=>'January','February','March','April','May',
                   
'June','July','August','September','October','November','December'),
                   
                   
"short" => array(1=>'Jan','Feb','Mar','Apr','May','Jun',
                   
'Jul','Aug','Sep','Oct','Nov','Dec')
                ),
               
"es" => array(
                   
"full" => array(1=>'Enero','Febrero','Marzo','Abril','Mayo',
                   
'Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Deciembre'),
                   
                   
"short" => array(1=>'Ene','Feb','Mar','Abr','May','Jun',
                   
'Jul','Ago','Sep','Oct','Nov','Dec')
                ),
            );
           
           
$exploded = explode("-",$date);
           
$year = $exploded[0];
           
$month = $exploded[1];
           
$day = $exploded[2];
           
           
$month = $monthNames[$locale][$length][$month];
           
$date = $day . " " . $month . ", " . $year;
            return
$date;
    }

?>
up
6
aydinantmen [at] hotmail [dot] com
1 year ago
I want to use multidimentional arrays in a callback function what accepts second parameter.

Solution:

<?php

$arr1
= array("a" => "b", "c", "d's", "e" => array("f's", "g" => array("h's", "i" => "j's")));
$arr2 = mdarr_parameter($arr1);
$arr3 = mdarr_parameter($arr2, true);

function
mdarr_parameter($needle, $job=false) {
    if (
is_array($needle)) {
        foreach(
$needle as $name => $value) {
           
$needle[$name] = mdarr_parameter($value, $job);
        }
    } else {
       
// Now you do anything you want...
       
if ($job === true) {
           
$needle = stripslashes($needle);
        } else {
           
$needle = addslashes($needle);
        }
    }
    return
$needle;
}

print_r($arr2);
print_r($arr3);

/**
Outputs:

Array
(
    [a] => b
    [0] => c
    [1] => d\'s
    [e] => Array
        (
            [0] => f\'s
            [g] => Array
                (
                    [0] => h\'s
                    [i] => j\'s
                )

        )

)
Array
(
    [a] => b
    [0] => c
    [1] => d's
    [e] => Array
        (
            [0] => f's
            [g] => Array
                (
                    [0] => h's
                    [i] => j's
                )

        )

)
**/

?>
up
0
php at xenhideout dot nl
1 year ago
Please be advised that the code block defining the function, within the function_exists() call, has to be executed for the function to get defined, whereas this is not the case for regular, unenclosed functions.

Meaning, if you write code like this:

<?php

do_function
();

if (!
function_exists('my_undefined')) {
    function
my_undefined() {
    }
}

function
do_function() {
   
my_undefined();
}
?>

..Then my_undefined will not be defined before the code in do_function calls it. Some people put their function sections below the regular executing code of the script. Making any of it 'pluggable' can then cause problems.
up
-1
info at namasteui dot com
1 month ago
Functions that are written by the user are User defined functions.

function function name [(argument1, argument 2, ...argument n)]
{any PHP code }

For example,

<?php
function hello()
{
print(
"Hello!");
}
hello();
?>

Using the function hello() anywhere in the PHP code will display the word "Hello".
up
-1
ohcc at 163 dot com
2 months ago
As of PHP 7.0, you can restrain type of return value of user defined functions.

Syntax is : function FunctionName ($arg1, $arg2, ...)  : TYPE { ... }

TYPE is a string representing the type of return value, TYPE can be a class name or a php variable type, such as array/string/bool/int/float.

When TYPE is one of the following value, it also stands for a classname

str/boolean/integer/real/double/resource/object/scalar

However,in my opion, boolean/bool, integer/int ... should have the same meaning, but at least in PHP7, they stand for different meanings respectively. This may be fixed in later versions of PHP.

<?php
   
function wxc ($var) : string {
        return
$var;
    }
?>

this function must return a string, if it return something else when called, a "Fatal error: Uncaught TypeError" error will be triggered.

code above is supported only in PHP 7+
up
-2
N Atanackovic
10 months ago
You can also call function from itself.  For example, I want to reach the deepest value in multidimensional array and I call function from inside the very same function. In this example function behave as some meta-loop.
   
<?php

$arr1
=array('a'=>array('e'=>array('f'=>array('g'=>'h', 'n' )),'b','c'));
$arr2=array('a'=>array('e'=>array('f'=>array('g'=>array('l'=>array('m'=>'w','q')), 'n' )),'b','c'));

function
Deep($array){
    foreach(
$array as $key){
        if(
is_array($key)){
             return
Deep($key);//calling the function inside the function
}else {
echo
$key;
        }
    }
}

echo
Deep($arr1); //outputs: hn
echo Deep($arr2); //outputs: wq

?>
To Top