Example #1
0
{
    $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
#Example #3 Functions within functions

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. */
Example #2
0
[expect] Warning:
[expect] Missing argument #2
[expect]
Making a bowl of raspberry .

[file]
<?php 
function makeyogurt($type = "acidophilus", $flavour)
{
    return "Making a bowl of {$type} {$flavour}.\n";
}
echo makeyogurt("raspberry");
// won't work as expected
Example #3
0
{
    return "Making a cup of {$type}.\n";
}
echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");
echo "<hr>";
echo "<b>Incorrect usage of default function arguments</b>\n";
function makeyogurt($type = "acidophilus", $flavour)
{
    return "Making a bowl of {$type} {$flavour}.\n";
}
//Warning:  Missing argument 2 for makeyogurt()
echo makeyogurt("raspberry");
// won't work as expected
echo makeyogurt("raspberry", "flavour");
// won't work as expected
echo "<hr>";
echo "<b>Variable-length argument lists PHP5.6</b><hr>";
//PHP 5.6
/*
function sum(...$numbers) {
  $acc = 0;
  foreach ($numbers as $n) {
    $acc += $n;
  }
  return $acc;
}
echo sum(1, 2, 3, 4);
*/
echo "<b>Using ... to provide arguments PHP5.6</b><hr>";
Example #4
0
    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 
function makeyogurt1($flavour, $type = "access")
{
    return "Making a bowl of {$type},,,,, {$flavour}.\n";
}
echo makeyogurt1("dddddddddd");
?>

<!--返回一个数组已得到多个返回值-->
<?php 
function small_numbers()
{
Example #5
0
<?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
?>