2.10.PHP7.1 女神级教程-女神的私人信息 -【PHP 函数】

来源:互联网 发布:mill9%2e1编程教程 编辑:程序博客网 时间:2024/04/28 07:25

目录

博客目录

http://www.foxwho.com/article/24

CSDN目录

http://blog.csdn.net/fenglailea/article/details/60330101

风.fox

PHP 函数

函数由 function 关键词和自定义名称表示

function test($msg,$other){    return "参数1:{$msg},参数2:{$other}";}

test 就是一个函数
$msg 就是 这个函数的第一个参数
$other就是 这个函数的第二个参数
$msg$other可是任何数据类型,可以是数值,字符,逻辑,对象,数组,null值等等

函数调用

echo test("这是函数","其他");//返回正确信息echo test();//PHP程序报错,参数必须有值

在直接调用test()函数时,函数的参数没有传入任何值,那么就会报错,如果你不想直接调用无参数的函数不报错,那么久要给参数增加默认值

以下函数的参数就增加了字符串空值

function test($msg='',$other=''){    return "参数1:{$msg},参数2:{$other}";}

更严格的PHP函数参数

函数变量类型声明 两种模式 : 强制 ( 默认 ) 和 严格模式

默认模式 如刚开始的PHP函数

类型:array,object(对象),string、int、float和 bool

class Product{    public function name(){        return "书名称";    }}function test(string $msg='',array $option=[],int $int=0, float $float=1.2,bool  $bool=false,Product $product=null){    return "参数1:{$msg},参数2:{$other}";}//调用test("字符串",['min'=>10],100,98.90,true,new Product());

string $msg 传输的参数必须是字符串

array $option 传输的参数必须是数组

int $int 传输的参数必须是整数

float $float 传输的参数必须是浮点数

bool $bool 传输的参数必须是 逻辑数

Product $product 传输的参数必须 Product对象

php 返回值类型声明

函数和匿名函数都可以指定返回值的类型

function show(): array{     return [1,2,3,4]; }function arraysSum(array ...$arrays): array{return array_map(function(array $array): int {return array_sum($array);}, $arrays);}

PHP 数组返回值 array

function show(): array{     return [1,2,3,4]; }

PHP 字符串返回值 string

function show(): string{     return "ABCDEF";}

PHP 整数返回值 int

function show(): int{     return 9999;}

PHP 浮点数返回值 float

function show(): float{     return 11.1;}

PHP 逻辑返回值 bool

function show(): bool{     return true;}

PHP 对象返回值 object

class Product{    public function name(){        return "书名称";    }}function show(): Product{     return new Product();}

php void返回值 Void 函数

在PHP 7 中引入的其他返回值类型的基础上,一个新的返回值类型void被引入。 返回值声明为 void 类型的方法要么干脆省去 return 语句,要么使用一个空的 return 语句。 对于 void 函数来说,null 不是一个合法的返回值。

function swap(&$left, &$right) : void{    if ($left === $right) {        return;    }    $tmp = $left;    $left = $right;    $right = $tmp;}$a = 1;$b = 2;var_dump(swap($a, $b), $a, $b);

以上例程会输出:

nullint(2)int(1)

试图去获取一个 void 方法的返回值会得到 null ,并且不会产生任何警告。这么做的原因是不想影响更高层次的方法。

PHP 函数 的参数跳跃

如果你有一个函数接受多个可选的参数,目前没有办法只改变最后一个参数,而让其他所有参数为默认值。

function lists($where, $order_by,$field='',$page=1, $page_size=20) {    ...}

那么有没有办法设置$page_size=30,而其他两个为默认值。为了解决这个跳跃参数的问题而提出:

lists("deleted=0", "name", default, default, 30);

PHP 可变函数参数

代替 func_get_args()

function add(...$args)  {      $result = 0;      foreach($args as $arg)          $result += $arg;      return $result;  } 

PHP 函数 可为空(Nullable)类型

类型现在允许为空,当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null 。可以通过在类型前面加上一个问号来使之成为可为空的。

function test(?string $name){    var_dump($name);}

以上例程会输出:

string(5) "tpunt"NULLUncaught Error: Too few arguments to function test(), 0 passed in...

PHP 函数 参数解包功能

在调用函数的时候,通过 … 操作符可以把数组或者可遍历对象解包到参数列表,这和Ruby等语言中的扩张(splat)操作符类似

function add($a, $b, $c) {      return $a + $b + $c;  }  $arr = [2, 3];  add(1, ...$arr);

PHP 函数 Callable typehint

对PHP函数,类的方法调用

function foo(callable $callback) {  }

案例

foo("false"); //错误,因为false不是callable类型    foo("printf"); //正确    foo(function(){}); //正确  class A {    static function show() {      }  }    foo(array("A", "show")); //正确

参考
http://blog.csdn.net/fenglailea/article/details/52717646

0 0
原创粉丝点击