06c07-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 35 行

PHP
35
字号
<?php// Define an 'interesting' function that does different math based //  on the number of argumentsfunction interesting() {    // Read in the number of arguments:    $args = func_num_args();        // If one argument, square it and return it    if ($args == 1) {        $num = func_get_arg(0);        return $num * $num;    }    // If two arguments, multiply them and return it:    elseif ($args == 2) {        return func_get_arg(0) * func_get_arg(1);    }    // Otherwise, sum up all the values and return them    else {        $nums = func_get_args();        return array_sum($nums);    }}// Try the function in all 3 forms:// Pass it a 2, and it should return 4echo '<p>', interesting(2), '</p>';// Pass it a 2 and a 4, and it should return 8echo '<p>', interesting(2, 4), '</p>';// Pass it a 2, 4, 1, and 8, and it will add them to get 15echo '<p>', interesting(2,4,1,8), '</p>';?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?