在php中,error_reporting() 设置 PHP 的报错级别并返回当前级别。
语法:error_reporting(report_level)
如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:
; 错误报告是按位的。或者将数字加起来得到想要的错误报告等级。
; E_ALL – 所有的错误和警告
; E_ERROR – 致命性运行时错
; E_WARNING – 运行时警告(非致命性错)
; E_PARSE – 编译时解析错误
; E_NOTICE – 运行时提醒(这些经常是是你的代码的bug引起的,
;也可能是有意的行为造成的。(如:基于未初始化的变量自动初始化为一个
;空字符串的事实而使用一个未初始化的变量)
; E_CORE_ERROR – 发生于PHP启动时初始化过程中的致命错误
; E_CORE_WARNING – 发生于PHP启动时初始化过程中的警告(非致命性错)
; E_COMPILE_ERROR – 编译时致命性错
; E_COMPILE_WARNING – 编译时警告(非致命性错)
; E_USER_ERROR – 用户产生的出错消息
; E_USER_WARNING – 用户产生的警告消息
; E_USER_NOTICE – 用户产生的提醒消息
任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。
例如,以下代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:
例1,
<?php
//禁用错误报告
error_reporting(0);
//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
error_reporting(E_ALL);
?>
例2,
<?php error_reporting(255); // 列出所有提示 error_reporting(0); // 不显示所有提示 error_reporting(7); // 建议使用 ?>
所谓php时间轴,即显示为“刚刚”、“5分钟前”、“昨天10:30”等,很多的论坛、博客中均有类似的功能,看来用的还是相当普遍。
代码如下:
<?php
/**
* php时间轴开发
* edit by www.
*/
function tranTime($time) {
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
if ($time < 60) {
$str = '刚刚';
}
elseif ($time < 60 * 60) {
$min = floor($time/60);
$str = $min.'分钟前';
}
elseif ($time < 60 * 60 * 24) {
$h = floor($time/(60*60));
$str = $h.'小时前 '.$htime;
}
elseif ($time < 60 * 60 * 24 * 3) {
$d = floor($time/(60*60*24));
if($d==1)
$str = '昨天 '.$rtime;
else
$str = '前天 '.$rtime;
}
else {
$str = $rtime;
}
return $str;
}
?>
代码说明:
函数tranTime()中的参数$time必须为Unix时间戳,其它类型的时间,先用strtotime()将其转换成Unix时间戳。
调用示例:
<?php //php时间轴例子 $times=”1286861696 “; echo tranTime($times); ?>
有关判断手机类型的方法,之前的文章有所提及。
相关参考文章:
判断是否手机访问
php判断是否手机访问的代码
用HTTP_USER_AGENT判断手机类型,代码如下:
<?php
/**
* 判断手机类型
* by www.
*/
function is_mobile_request()
{
$_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
$mobile_browser = '0';
if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap
|phone|iphone|ipad|ipod|android|xoom)/i',
strtolower($_SERVER['HTTP_USER_AGENT'])))
$mobile_browser++;
if((isset($_SERVER['HTTP_ACCEPT'])) and (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false))
$mobile_browser++;
if(isset($_SERVER['HTTP_X_WAP_PROFILE']))
$mobile_browser++;
if(isset($_SERVER['HTTP_PROFILE']))
$mobile_browser++;
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-'
);
if(in_array($mobile_ua, $mobile_agents))
$mobile_browser++;
if(strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false)
$mobile_browser++;
// Pre-final check to reset everything if the user is on Windows
if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') !== false)
$mobile_browser=0;
// But WP7 is also Windows, with a slightly different characteristic
if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false)
$mobile_browser++;
if($mobile_browser>0)
return true;
else
return false;
}
?>
附一个自己写的简单方法:
<?php
//能过http_user_agent检测是否手机访问
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|
philips|panasonic|alcatel|lenovo|cldc|midp|mobile|wap)/i";
if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
{
$Loaction = 'wap/';
if (!empty($Loaction))
{
ecs_header("Location: $Loaction\n");
exit;
}
}
?>