收藏 分销(赏)

Php使用编码技巧篇之一.doc

上传人:仙人****88 文档编号:11731489 上传时间:2025-08-11 格式:DOC 页数:5 大小:37KB 下载积分:10 金币
下载 相关 举报
Php使用编码技巧篇之一.doc_第1页
第1页 / 共5页
Php使用编码技巧篇之一.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
Php使用编码技巧篇之一 即使使用 PHP 多年,也会偶然发现一些未曾了解的函数和功能。其中有些是非常有用的,但没有得到充分利用。并不是所有人都会从头到尾一页一页地阅读手册和函数参考! 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: */ 现在让我们看看如何建立能够接受任何参数数目的函数。这一次需要使用 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).": $v\n";  } } 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 ) */ 请注意,这些文件其实是可以返回一个路径,这取决于查询条件: 以下为引用的内容: $files = glob('../images/a*.jpg'); print_r($files); /* output looks like: Array (     [0] => ../images/apple.jpg     [1] => ../images/art.jpg ) */ 如果你想获得每个文件的完整路径,你可以调用 realpath() 函数: 以下为引用的内容: $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 ) */ 3魔术常量 PHP 提供了获取当前行号 (__LINE__)、文件路径 (__FILE__)、目录路径 (__DIR__)、函数名 (__FUNCTION__)、类名 (__CLASS__)、方法名 (__METHOD__) 和命名空间 (__NAMESPACE__) 等有用的魔术常量。在这篇文章中不作一一介绍,但是我将告诉你一些用例。当包含其他脚本文件时,使用 __FILE__ 常量(或者使用 PHP5.3 新具有的 __DIR__ 常量): 以下为引用的内容: // this is relative to the loaded script's path // it may cause problems when running scripts from different directories require_once('config/database.php'); // this is always relative to this file's path // no matter where it was included from require_once(dirname(__FILE__) . '/config/database.php'); 使用 __LINE__ 使得调试更为轻松。你可以跟踪到具体行号。 以下为引用的内容: // some code // ... my_debug("some debug message", __LINE__); /* prints Line 4: some debug message */ // some more code // ... my_debug("another debug message", __LINE__); /* prints Line 11: another debug message */ function my_debug($msg, $line) {  echo "Line $line: $msg 投稿人:童绍乐 时间:2010-5-28
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服