PHP中9个必须知道的实用函数及功能应用
2016-03-18 11:09
603 查看
最新PHP中9个必须知道的实用函数及功能应用以下是三零网为大家整理的最新PHP中9个必须知道的实用函数及功能应用的文章,希望大家能够喜欢!即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考!
1、任意参数数目的函数你可能已经知道,PHP 允许定义可选参数的函数。但也有完全允许任意数目的函数参数的方法。以下是可选参数的例子:
现在让我们看看如何建立能够接受任何参数数目的函数。这一次需要使用 func_get_args() 函数:
2、使用 Glob() 查找文件许多 PHP 函数具有长描述性的名称。然而可能会很难说出 glob() 函数能做的事情,除非你已经通过多次使用并熟悉了它。可以把它看作是比 scandir() 函数更强大的版本,可以按照某种模式搜索文件。
你可以像这样获得多个文件:
请注意,这些文件其实是可以返回一个路径,这取决于查询条件:
如果你想获得每个文件的完整路径,你可以调用 realpath() 函数:
转载来自:http://www.q3060.com/list3/list117/283.html
1、任意参数数目的函数你可能已经知道,PHP 允许定义可选参数的函数。但也有完全允许任意数目的函数参数的方法。以下是可选参数的例子:
// function with 2 optional arguments function foo($arg1 = '', $arg2 = ''){ echo "arg1: $arg1\n"; echo "arg2: $arg2\n"; }foo('hello', 'world'); /** prints: arg1: hello arg2: world */foo(); /** prints: arg1: arg2: */ |
// 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) . ": $v\n"; } }foo(); /** prints nothing */foo('hello'); /** prints arg1: hello */foo('hello', 'world', 'again'); /** prints arg1: hello arg2: world arg3: again */ |
// 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 ) */ |
$files = glob('../images/a*.jpg');print_r($files); /** output looks like: Array ( [0] => ../images/apple.jpg [1] => ../images/art.jpg ) */ |
$files = glob('../images/a*.jpg');// applies the function to each array element $files = array_map('realpath', $files);print_r($files); /** output looks like: Array ( [0] => C:\wamp\www\images\apple.jpg [1] => C:\wamp\www\images\art.jpg ) */ |
相关文章推荐
- 「Linux 中国」2018 微信文章排行榜
- 企业批量部署RealVNC远程控制软件
- C#编程自学之开篇介绍
- 文章推荐系统(二)
- 基于php的CMS中展示文章类实例分析
- 不用IDE写C#的Hello World的方法
- 在WordPress中使用wp_count_posts函数来统计文章数量
- jquery Moblie入门―hello world的示例代码学习
- 文章推荐系统(三)
- 一篇入门的php Class 文章
- 推荐文章系统(一)
- Android学习笔记(一)环境安装及第一个hello world
- php实现检查文章是否被百度收录
- Cocos2d-x学习笔记之Hello World!
- hello world程序集锦
- 精选文章推荐汇总-2018.03.01
- 精选文章汇总-2018.1.26
- Logstash笔记(二) ----input插件
- linux操练之---------vi/vim去除历史查找结果高亮显示
- PHP字符串之间的比较