锟斤拷锟斤拷锟侥诧拷锟斤拷

通锟斤拷锟斤拷锟斤拷锟叫憋拷锟斤拷源锟斤拷锟斤拷锟较拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷远锟斤拷锟斤拷锟轿拷指锟斤拷锟斤拷谋锟斤拷式锟叫憋拷锟斤拷锟斤拷锟角达拷锟斤拷锟斤拷锟斤拷锟斤拷值锟侥★拷

PHP 支锟街帮拷值锟斤拷锟捷诧拷锟斤拷锟斤拷默锟较o拷锟斤拷通锟斤拷锟斤拷锟矫达拷锟捷诧拷锟斤拷锟皆硷拷默锟较诧拷锟斤拷锟斤拷也支锟斤拷锟缴变长锟饺诧拷锟斤拷锟叫憋拷锟斤拷

Example #1 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷

<?php
function takes_array($input)
{
    echo 
"$input[0] + $input[1] = "$input[0]+$input[1];
}
?>

通锟斤拷锟斤拷锟矫达拷锟捷诧拷锟斤拷

默锟斤拷锟斤拷锟斤拷拢锟斤拷锟斤拷锟斤拷锟斤拷锟酵拷锟街碉拷锟斤拷荩锟斤拷锟斤拷锟斤拷使锟节猴拷锟斤拷锟节诧拷锟侥憋拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥变函锟斤拷锟解部锟斤拷值锟斤拷锟斤拷锟斤拷锟较o拷锟斤拷锟斤拷锟斤拷锟斤拷薷锟斤拷锟斤拷牟锟斤拷锟街碉拷锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷么锟斤拷莶锟斤拷锟斤拷锟�

锟斤拷锟斤拷锟揭拷锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷么锟斤拷荩锟斤拷锟斤拷锟斤拷诤锟斤拷锟斤拷锟斤拷锟斤拷懈貌锟斤拷锟斤拷锟角帮拷锟斤拷锟较凤拷锟斤拷 &锟斤拷

Example #2 锟斤拷锟斤拷锟矫达拷锟捷猴拷锟斤拷锟斤拷锟斤拷

<?php
function add_some_extra(&$string)
{
    
$string .= 'and something extra.';
}
$str 'This is a string, ';
add_some_extra($str);
echo 
$str;    // outputs 'This is a string, and something extra.'
?>

默锟较诧拷锟斤拷锟斤拷值

锟斤拷锟斤拷锟斤拷锟皆讹拷锟斤拷 C++ 锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷默锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷示锟斤拷

Example #3 锟节猴拷锟斤拷锟斤拷使锟斤拷默锟较诧拷锟斤拷

<?php
function makecoffee($type "cappuccino")
{
    return 
"Making a cup of $type.\n";
}
echo 
makecoffee();
echo 
makecoffee(null);
echo 
makecoffee("espresso");
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.

PHP 锟斤拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷 array 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 NULL 锟斤拷为默锟较诧拷锟斤拷锟斤拷锟斤拷锟界:

Example #4 使锟矫非憋拷锟斤拷锟斤拷锟斤拷锟斤拷为默锟较诧拷锟斤拷

<?php
function makecoffee($types = array("cappuccino"), $coffeeMaker NULL)
{
    
$device is_null($coffeeMaker) ? "hands" $coffeeMaker;
    return 
"Making a cup of ".join(", "$types)." with $device.\n";
}
echo 
makecoffee();
echo 
makecoffee(array("cappuccino""lavazza"), "teapot");
?>

默锟斤拷值锟斤拷锟斤拷锟角筹拷锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟皆憋拷锟斤拷锟斤拷吆锟斤拷锟斤拷锟斤拷玫取锟�

注锟解当使锟斤拷默锟较诧拷锟斤拷时锟斤拷锟轿猴拷默锟较诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷魏畏锟侥拷喜锟斤拷锟斤拷锟斤拷也啵伙拷锟斤拷颍锟斤拷锟斤拷锟斤拷锟斤拷岚达拷锟皆わ拷诘锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷拇锟斤拷锟狡拷希锟�

Example #5 锟斤拷锟斤拷默锟较诧拷锟斤拷锟侥诧拷锟斤拷确锟矫凤拷

<?php
function makeyogurt($type "acidophilus"$flavour)
{
    return 
"Making a bowl of $type $flavour.\n";
}

echo 
makeyogurt("raspberry");   // won't work as expected
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Warning: Missing argument 2 in call to makeyogurt() in 
/usr/local/etc/httpd/htdocs/phptest/functest.html on line 41
Making a bowl of raspberry .

锟斤拷锟节o拷锟饺斤拷锟斤拷锟斤拷锟斤拷锟斤拷雍锟斤拷锟斤拷锟斤拷锟接o拷

Example #6 锟斤拷锟斤拷默锟较诧拷锟斤拷锟斤拷确锟斤拷锟矫凤拷

<?php
function makeyogurt($flavour$type "acidophilus")
{
    return 
"Making a bowl of $type $flavour.\n";
}

echo 
makeyogurt("raspberry");   // works as expected
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

Making a bowl of acidophilus raspberry.

Note: 锟斤拷 PHP 5 锟金,达拷锟斤拷锟矫的诧拷锟斤拷也锟斤拷锟斤拷锟斤拷默锟斤拷值锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷

Note:

锟斤拷PHP 5锟叫o拷锟斤拷锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷为锟斤拷锟斤拷锟斤拷示锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟节碉拷锟斤拷时要锟斤拷锟斤拷锟轿拷囟锟斤拷锟斤拷汀锟� 锟斤拷锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷筒锟斤拷裕锟斤拷锟矫达拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷 锟斤拷PHP 5锟叫o拷锟解将锟斤拷一锟斤拷锟缴恢革拷锟斤拷锟斤拷锟斤拷锟斤拷锟襟,讹拷锟斤拷PHP 7锟叫斤拷锟斤拷锟阶筹拷一锟斤拷TypeError锟届常锟斤拷

为锟斤拷指锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷应锟矫加碉拷锟斤拷锟斤拷锟斤拷前锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥拷锟街碉拷锟轿�NULL锟斤拷实锟斤拷锟斤拷锟斤拷锟斤拷NULL锟斤拷

Valid types

Type Description Minimum PHP version
Class/interface name The parameter must be an instanceof the given class or interface name. PHP 5.0.0
self The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. PHP 5.0.0
array The parameter must be an array. PHP 5.1.0
callable The parameter must be a valid callable. PHP 5.4.0
bool The parameter must be a boolean value. PHP 7.0.0
float The parameter must be a floating point number. PHP 7.0.0
int The parameter must be an integer. PHP 7.0.0
string The parameter must be a string. PHP 7.0.0
Warning

Aliases for the above scalar types are not supported. Instead, they are treated as class or interface names. For example, using boolean as a parameter or return type will require an argument or return value that is an instanceof the class or interface boolean, rather than of type bool:

<?php
 
function test(boolean $param) {}
 
test(true);
 
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

 Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of boolean, boolean given, called in - on line 1 and defined in -:1
 

锟斤拷锟斤拷

Example #7 Basic class type declaration

<?php
class {}
class 
extends {}

// This doesn't extend C.
class {}

function 
f(C $c) {
    echo 
get_class($c)."\n";
}

f(new C);
f(new D);
f(new E);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

C
D

Fatal error: Uncaught TypeError: Argument 1 passed to f() must be an instance of C, instance of E given, called in - on line 14 and defined in -:8
Stack trace:
#0 -(14): f(Object(E))
#1 {main}
  thrown in - on line 8

Example #8 Basic interface type declaration

<?php
interface { public function f(); }
class 
implements { public function f() {} }

// This doesn't implement I.
class {}

function 
f(I $i) {
    echo 
get_class($i)."\n";
}

f(new C);
f(new E);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

C

Fatal error: Uncaught TypeError: Argument 1 passed to f() must implement interface I, instance of E given, called in - on line 13 and defined in -:8
Stack trace:
#0 -(13): f(Object(E))
#1 {main}
  thrown in - on line 8

Example #9 Nullable type declaration

<?php
class {}

function 
f(C $c null) {
    
var_dump($c);
}

f(new C);
f(null);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

object(C)#1 (0) {
}
NULL

锟较革拷锟斤拷锟斤拷

默锟斤拷锟斤拷锟斤拷拢锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥伙拷锟斤拷PHP锟斤拷锟斤拷强锟饺达拷锟斤拷锟斤拷锟酵碉拷值转为锟斤拷锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟酵★拷 锟斤拷锟界,一锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷string锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�integer锟斤拷锟斤拷锟秸猴拷锟斤拷锟矫碉拷锟侥斤拷锟斤拷锟斤拷一锟斤拷string锟斤拷锟酵碉拷值锟斤拷

锟斤拷锟皆伙拷锟斤拷每一锟斤拷锟侥硷拷锟斤拷锟斤拷锟较革拷模式锟斤拷锟斤拷锟较革拷模式锟叫o拷只锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷全锟斤拷锟斤拷谋锟斤拷锟斤拷呕岜伙拷锟斤拷埽锟斤拷锟斤拷蚪锟斤拷壮锟揭伙拷锟�TypeError锟斤拷 唯一锟斤拷一锟斤拷锟斤拷锟斤拷锟角匡拷锟皆斤拷integer锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷float锟侥猴拷锟斤拷锟斤拷

使锟斤拷 declare 锟斤拷锟斤拷strict_types 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较革拷模式锟斤拷

Caution

锟斤拷锟斤拷锟较革拷模式同时也锟斤拷影锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷.

Note:

锟较革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟较革拷模式锟斤拷锟侥硷拷锟斤拷锟侥猴拷锟斤拷锟斤拷锟矫o拷锟斤拷锟斤拷锟斤拷锟斤拷锟角革拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟侥猴拷锟斤拷锟斤拷 一锟斤拷没锟斤拷锟斤拷锟斤拷锟较革拷模式锟斤拷锟侥硷拷锟节碉拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟较革拷模式锟斤拷锟侥硷拷锟叫讹拷锟斤拷暮锟斤拷锟斤拷锟斤拷锟矫达拷锟斤拷锟斤拷锟窖拷锟斤拷锟斤拷叩锟狡拷茫锟斤拷锟斤拷锟斤拷停锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟结被转锟斤拷锟斤拷

Note:

锟较革拷锟斤拷锟酵斤拷锟斤拷锟节憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷也锟斤拷锟斤拷锟斤拷为锟斤拷耍锟斤拷锟斤拷锟揭狿HP 7.0.0 锟斤拷锟斤拷掳姹撅拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭诧拷锟斤拷锟斤拷歉锟斤拷姹撅拷锟斤拷锟接的★拷

Example #10 Strict typing

<?php
declare(strict_types=1);

function 
sum(int $aint $b) {
    return 
$a $b;
}

var_dump(sum(12));
var_dump(sum(1.52.5));
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

int(3)

Fatal error: Uncaught TypeError: Argument 1 passed to sum() must be of the type integer, float given, called in - on line 9 and defined in -:4
Stack trace:
#0 -(9): sum(1.5, 2.5)
#1 {main}
  thrown in - on line 4

Example #11 Weak typing

<?php
function sum(int $aint $b) {
    return 
$a $b;
}

var_dump(sum(12));

// These will be coerced to integers: note the output below!
var_dump(sum(1.52.5));
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

int(3)
int(3)

Example #12 Catching TypeError

<?php
declare(strict_types=1);

function 
sum(int $aint $b) {
    return 
$a $b;
}

try {
    
var_dump(sum(12));
    
var_dump(sum(1.52.5));
} catch (
TypeError $e) {
    echo 
'Error: '.$e->getMessage();
}
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

int(3)
Error: Argument 1 passed to sum() must be of the type integer, float given, called in - on line 10

锟缴憋拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟叫憋拷

PHP 锟斤拷锟矫伙拷锟皆讹拷锟藉函锟斤拷锟斤拷支锟街可憋拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟叫憋拷锟斤拷 PHP 5.6 锟斤拷锟斤拷锟较的版本锟叫o拷锟斤拷 ... 锟斤法实锟街o拷锟斤拷 PHP 5.5 锟斤拷锟斤拷锟斤拷姹撅拷校锟绞癸拷煤锟斤拷锟� func_num_args()锟斤拷func_get_arg()锟斤拷锟斤拷 func_get_args() 锟斤拷

... in PHP 5.6+

In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; for example:

Example #13 Using ... to access variable arguments

<?php
function sum(...$numbers) {
    
$acc 0;
    foreach (
$numbers as $n) {
        
$acc += $n;
    }
    return 
$acc;
}

echo 
sum(1234);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

10

You can also use ... when calling functions to unpack an array or Traversable variable or literal into the argument list:

Example #14 Using ... to provide arguments

<?php
function add($a$b) {
    return 
$a $b;
}

echo 
add(...[12])."\n";

$a = [12];
echo 
add(...$a);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

3
3

You may specify normal positional arguments before the ... token. In this case, only the trailing arguments that don't match a positional argument will be added to the array generated by ....

It is also possible to add a type hint before the ... token. If this is present, then all arguments captured by ... must be objects of the hinted class.

Example #15 Type hinted variable arguments

<?php
function total_intervals($unitDateInterval ...$intervals) {
    
$time 0;
    foreach (
$intervals as $interval) {
        
$time += $interval->$unit;
    }
    return 
$time;
}

$a = new DateInterval('P1D');
$b = new DateInterval('P2D');
echo 
total_intervals('d'$a$b).' days';

// This will fail, since null isn't a DateInterval object.
echo total_intervals('d'null);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

3 days
Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2

Finally, you may also pass variable arguments by reference by prefixing the ... with an ampersand (&).

Older versions of PHP

No special syntax is required to note that a function is variadic; however access to the function's arguments must use func_num_args(), func_get_arg() and func_get_args().

The first example above would be implemented as follows in PHP 5.5 and earlier:

Example #16 Accessing variable arguments in PHP 5.5 and earlier

<?php
function sum() {
    
$acc 0;
    foreach (
func_get_args() as $n) {
        
$acc += $n;
    }
    return 
$acc;
}

echo 
sum(1234);
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

10