Ejemplo n.º 1
0
<?php

function xt_sum($a, $b)
{
    return $a + $b;
}
echo xt_sum(4, 5), "\n";
//可变参数
function sum(...$numbers)
{
    $acc = 0;
    foreach ($numbers as $n) {
        $acc += $n;
    }
    return $acc;
}
echo sum(1, 2, 3, 4) . "\n";
//数组解包
echo sum(...[1, 2, 3, 4, 5]) . "\n";
//默认参数
function makecoffee($type = "cappuccino")
{
    return "Making a cup of {$type}.\n";
}
echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");
Ejemplo n.º 2
0
<?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");
Ejemplo n.º 3
0
    return "Making a cup of $type.\n";
}
echo makecoffee(); //Making a cup of cappuccino.
echo makecoffee(null); //Making a cup of .
echo makecoffee("espresso"); //Making a cup of espresso.

?>
<?php
#Example #4 Using non-scalar types as default values
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(); //Making a cup of cappuccino with hands
echo makecoffee(array("cappuccinoooo", "lavazza"), "teapot"); // Making a cup of cappuccinoooo,
// lavazza with teapot
?>
<?php
#Example #5 Incorrect usage of default function arguments
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 for makeyogurt(), called in 
//Notice: Undefined variable: flavour in
//Making a bowl of raspberry.

?>
<?php
Ejemplo n.º 4
0
<?php
#Example #3 Use of default parameters in functions
function makecoffee($type = "cappuccino")
{
    return "Making a cup of $type.\n";
}
echo makecoffee(); //Making a cup of cappuccino.
echo makecoffee(null); //Making a cup of .
echo makecoffee("espresso"); //Making a cup of espresso.

?>
Ejemplo n.º 5
0
echo makecoffee1();
echo makecoffee1(null);
echo makecoffee1("xudedong"), "<BR>";
?>

<!--PHP 还允许使用数组 array 和特殊类型 NULL 作为默认参数,使用非标量类型作为默认参数,
默认值必须是常量表达式,不能是诸如变量,类成员,或者函数调用等。-->
<?php 
function makecoffee($types = array("aaaaa"), $coffeeMaker = NULL)
{
    $device = is_null($coffeeMaker) ? "null" : "hands,{$coffeeMaker}";
    return "Making a cup of " . join(",", $types) . " with {$device}.\n";
}
echo makecoffee(), "<BR>";
echo 1.111111111111111E+27, "<BR>";
echo makecoffee(array("aaaaa", "bbbbb", "ccc"), 'ttt');
?>

<!--注意当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧;否则,函数将不会按照预期的情况工作。-->

<!--函数默认参数的不正确用法-->
<?php 
function makeyogurt($type = "access", $flavour)
{
    return "Making a bowl of {$type}+++++++++++++++++++ {$flavour}.\n";
}
echo makeyogurt("dddddddddd"), "<BR>";
?>

<!--函数默认参数的正确用法-->
<?php 
Ejemplo n.º 6
0
<?php

define("BR", "</br>");
function add_some_extra(&$string)
{
    $string .= 'и кое-что еще.';
}
$str = 'Это строка, ';
add_some_extra($str);
echo $str;
// выведет 'Это строка, и кое-что еще.'
echo BR;
function makecoffee1($type = "капуччино")
{
    return "Готовим чашку {$type}.\n";
}
echo makecoffee1();
echo makecoffee1(null);
echo makecoffee1("эспрессо");
echo BR;
function makecoffee($types = array("капуччино"), $coffeeMaker = NULL)
{
    $device = is_null($coffeeMaker) ? "вручную" : $coffeeMaker;
    return "Готовлю чашку " . join(", ", $types) . " {$device}.\n";
}
echo makecoffee();
echo makecoffee(array("капуччино", "лавацца"), "в чайнике");