9个必须知道的适用PHP函数和性能
9个必须知道的适用PHP函数和性能
即使利用 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
)
*/
分享到: