用户自定义函数

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

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 层,因为可能会使堆栈崩溃从而使当前脚本终止。 无限递归可视为编程错误。

User Contributed Notes

ayyappan dot ashok at gmail dot com 31-Mar-2016 10:00
Type Checking in PHP 7

case A: //Using return
-------------------------
function welcome($name):string
{
   return $name;
}
echo welcome(100);

Results :  100

case B: //Using echo
-------------------------
function welcome($name):string
{
    echo $name;
}
welcome("100");

Results : Fatal error</b>:  Uncaught TypeError

function welcome(string $name)
{
    echo $name;
}
welcome(100);

Results :  100

case C: // Using strict_types
--------------------------------
declare(strict_types=1);

function welcome($name):string
{
    return $name;
}
echo welcome(90.99);

Results : Fatal error</b>:  Uncaught TypeError

case D: // Using strict_types
--------------------------------
//declare(strict_types=1);   On comment to strict_types

function welcome($name):string
{
    return $name;
}
echo welcome(90.99);

Results :  90.99

Note:
Behaviour of echo and return on welcome function show in different reflection. Representation of function type checking can be in either type as follows

function welcome(string $name){ }
or
function welcome($name):string{ }

Executed on PHP Version : 7.0.3
ayyappan dot ashok at gmail dot com 31-Mar-2016 03:45
//Calling function with in a function or inner function

function add($a,$b){
    return $a+$b;
}

function sub($a,$b){
    return $a-$b;
}

function math($first, $second) {
    $res =  add($first, $second)/sub($first, $second);
    return (int)$res;
}
echo math(200,100);  //Results  3
ayyappan dot ashok at gmail dot com 31-Mar-2016 03:22
For a good note, we can pass function as argument to function.

Take a look at the code. Passing function as argument can be achieved by Closure class ( A class to represent anonymous function).

function math(Closure $type, $first, $second) {
    // Execute the closure with parameters
    return $type($first, $second);
}

// Create an addition closure.
$addition = function ($first, $second) {
    // Add the values.
    return $first + $second;
};

// Create an subtraction closure.
$subtraction = function ($first, $second) {
    // Subtract the values.
    return $first - $second;
};

// Execute math function.
Note :anonymous  function is passed as an argument to function math.
echo math($addition, 2, 2);
echo PHP_EOL; // New line!
echo math($subtraction, 5, 3);

Courtesy : PHP Panda. Inspired from PHP panda.
ayyappan dot ashok at gmail dot com 31-Mar-2016 02:40
As posted by ohcc at 163 dot com

    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.

  But when executed by passing various datatypes, it doesn't throw error  
  other than array and object.

  Please look over the code

  function abc($var) :string { return $var;}
  echo abc(true);    // Results 1
 
  function abc($var) :string { return $var;}
  echo abc(88.99);  // Results 88.99

  function abc($var) :string { return $var;}
  echo abc(array());
 
  //Results
  Fatal error : Uncaught TypeError: Return value of abc() must be of the  
  type string

  Note :
  Even though function is forced to return only string, it still considers the  
  other datatype arguments as string.
 
  echo gettype(abc(99.88));  // Returns string.
info at namasteui dot com 30-Dec-2015 03:23
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".
Muneeb Aslam 24-Nov-2015 12:09
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;
    }

?>
ohcc at 163 dot com 08-Nov-2015 12:52
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+
N Atanackovic 17-Mar-2015 02:31
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

?>
php at xenhideout dot nl 16-Sep-2014 02:59
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.
aydinantmen [at] hotmail [dot] com 24-Apr-2014 02:17
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
                )

        )

)
**/

?>