展会信息港展会大全

9个必须知道的实用PHP函数和功能
来源:互联网   发布日期:2016-03-24 19:58:32   浏览:2525次  

导读:即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考!1、任意参数数目的函数你可能已经知...

即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考!

1、任意参数数目的函数

你可能已经知道,PHP 允许定义可选参数的函数。但也有完全允许任意数目的函数参数的方法。以下是可选参数的例子:

以下为引用的内容:

// function with 2 optional arguments

function foo($arg1 = '', $arg2 = '') {

echo "arg1: $arg1n";

echo "arg2: $arg2n";

}

foo('hello','world');

/* prints:

arg1: hello

arg2: world

*/

foo();

/* prints:

arg1:

arg2:

*/

现在让我们看看如何建立能够接受任何参数数目的函数。这一次需要使用 func_get_args() 函数:

以下为引用的内容:

// yes, the argument list can be empty

function foo() {

// returns an array of all passed arguments

$args = func_get_args();

foreach ($args as $k => $v) {

echo "arg".($k+1).": $vn";

}

}

foo();

/* prints nothing */

foo('hello');

/* prints

arg1: hello

*/

foo('hello', 'world', 'again');

/* prints

arg1: hello

arg2: world

arg3: again

*/

2、使用 Glob() 查找文件

许多 PHP 函数具有长描述性的名称。然而可能会很难说出 glob() 函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是比 scandir() 函数更强大的版本,可以按照某种模式搜索文件。

以下为引用的内容:

// get all php files

$files = glob('*.php');

print_r($files);

/* output looks like:

Array

(

[0] => phptest.php

[1] => pi.php

[2] => post_output.php

[3] => test.php

)

*/

你可以像这样获得多个文件:

以下为引用的内容:

// get all php files AND txt files

$files = glob('*.{php,txt}', GLOB_BRACE);

print_r($files);

/* output looks like:

Array

(

[0] => phptest.php

[1] => pi.php

[2] => post_output.php

[3] => test.php

[4] => log.txt

[5] => test.txt

)

*/

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港