当前位置: 编程技术>php
本页文章导读:
▪php中register_shutdown_function函数的用法 php函数register_shutdown_function的用法,如下:
<?php
function shutdown() {
$last_error = error_get_last();
if ($last_error) {
error_log(print_r($last_error, true), 3, ERROR_LOG);
}
}
register_shutdown_funct.........
▪php简单分页代码一例 代码:
<?php
//php分页
//by www.
$result = "<div page-num\"><ul fn-clear\">";
// 上一页
if ($offset>0) {
$result .= "<li>
<a href=/index.html"".$url.'offset='.($offset-$maxrow)."\"&g.........
▪while循环中不支持循环使用curl的实例代码 代码:
<?php
//while循环不支持循环curl
//by www.
$link = mysql_connect('localhost', 'sms', 'sms');
mysql_select_db('sms', $link);
mysql_query("set names utf8");
$sql = "
SELECT phone,chang, msg, linkid, mo_time FROM tables '";
.........
[1]php中register_shutdown_function函数的用法
来源: 互联网 发布时间: 2013-12-24
php函数register_shutdown_function的用法,如下:
<?php
function shutdown() {
$last_error = error_get_last();
if ($last_error) {
error_log(print_r($last_error, true), 3, ERROR_LOG);
}
}
register_shutdown_function('shutdown');
?>
说明:
在php脚本死掉时,不想给用户显示一个致命错误或一个空白页(在display_errors设为off的情况下) 。
PHP中有一个叫做 register_shutdown_function 的函数,可以让我们设置一个当执行关闭时可以被调用的另一个函数。
当脚本执行完成或意外死掉导致PHP执行即将关闭时,这个函数将会 被调用.所以,我们可以使用在脚本开始处设置一个变量为false,然后在脚本末尾将之设置为true的方法,让PHP关闭回调函数检查脚本完成与否. 如果我们的变量仍旧是false,我们就知道脚本的最后一行没有执行,因此它肯定在程序执行到某处死掉了。
以上例子演示在一个致命错误需要显示时,应该怎么给用户一些合适的反馈。
可以通过关闭致命错误的显示(译注:可以设置display_errors和 error_reporting)。
[2]php简单分页代码一例
来源: 互联网 发布时间: 2013-12-24
代码:
<?php
//php分页
//by www.
$result = "<div page-num\"><ul fn-clear\">";
// 上一页
if ($offset>0) {
$result .= "<li>
<a href=/index.html"".$url.'offset='.($offset-$maxrow)."\">Prev</a>
</li>";
}
$pages = $allPageNums; //总页数
$page = $curPage; //当前页数
$page_len = 9;
$page_len = ($page_len%2)?$page_len:$pagelen+1;//页码个数
$pageoffset = ($page_len-1)/2;//页码个数左右偏移量
if($pages>$page_len){
//如果当前页小于等于左偏移
if($page<=$pageoffset){
$init=1;
$max_p = $page_len;
}else{//如果当前页大于左偏移
//如果当前页码右偏移超出最大分页数
if($page+$pageoffset>=$pages+1){
$init = $pages-$page_len+1;
}else{
//左右偏移都存在时的计算
$init = $page-$pageoffset;
$max_p = $page+$pageoffset;
}
}
}
for($i=$init; $i<=$max_p; $i++) {
if ( $i == $curPage ) {
$result .= "<li on\"><a href=/index.html"".$url.'offset='.($i*$maxrow)."\" >$i</a></li>";
continue;
}
$result .= "<li><a href=/index.html"".$url.'offset='.(($i-1)*$maxrow)."\">$i</a></li>";
}
// 打印下一页
if ( $allnums > ($offset+$maxrow) ) {
$result .= "<li>
<a href=/index.html"".$url.'offset='.($offset+$maxrow)."\">Next</a>
</li>";
}
?>
[3]while循环中不支持循环使用curl的实例代码
来源: 互联网 发布时间: 2013-12-24
代码:
<?php
//while循环不支持循环curl
//by www.
$link = mysql_connect('localhost', 'sms', 'sms');
mysql_select_db('sms', $link);
mysql_query("set names utf8");
$sql = "
SELECT phone,chang, msg, linkid, mo_time FROM tables '";
$result = mysql_query($sql,$link);
$array = array();
while($row = mysql_fetch_array($result)){
$linkid = $row['phone'].date("YmdHis", strtotime($row['mo_time']));
$str = "SPNUM=".$row['chang']."&MOBILE=".$row['phone']."&CONTENT=".urlencode($row['msg'])."&MOTIME=".urlencode($row['mo_time']).
"&LINKID=".$linkid;
$url = "www.?".$str;
$array[] = $url;
//var_dump($url); 这里使用curl访问,只能访问一条随后就中断了,只能在上面存放到数组中
/*$result = file_get_contents($url);
var_dump($result);*/
/*$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// 4. 释放curl句柄
curl_close($ch);*/
}
//var_dump($array);
//这里使用foreach循环执行curl命令
foreach ($array as $url) {
var_dump($url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// 4. 释放curl句柄
curl_close($ch);
var_dump($result);
}
?>最新技术文章: