PHP函数和数,9个必须知道的实用PHP函数和功能

本文介绍了PHP中的一些实用但可能不为人知的函数和功能,如使用func_get_args()实现接受任意参数数目的函数,以及使用glob()进行模式匹配查找文件。通过示例代码,展示了如何有效地利用这些功能来提升PHP编程效率。
摘要由CSDN通过智能技术生成

9个必须知道的实用PHP函数和功能以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

a49f6e6fae4fc67f0399bf52e98f105c.png

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

任意参数数目的函数

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

以下为引用的内容

// function with optional arguments function foo($arg = $arg = ) {

echo "arg : $arg n";  echo "arg : $arg n";

}

foo( hello world ); /* prints: arg : hello arg : world */

foo(); /* prints: arg : arg : */

现在让我们看看如何建立能够接受任何参数数目的函数 这一次需要使用 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+ ) ": $vn";  }

}

foo(); /* prints nothing */

foo( hello ); /* prints arg : hello */

foo( hello world again ); /* prints arg : hello arg : world arg : again */

使用 Glob() 查找文件

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

以下为引用的内容

// get all php files $files = glob( * php );

print_r($files); /* output looks like: Array (     [ ] => phptest php     [ ] => pi php     [ ] => post_output php     [ ] => test php ) */

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

以下为引用的内容

// get all php files AND txt files $files = glob( * {php txt} GLOB_BRACE); lishixinzhi/Article/program/PHP/201311/21182

分页:123

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值