PHP中microtime()函数
南三方
首先介绍一下此函数的用法:
定义和用法(php5)
microtime() 函数返回当前 Unix 时间戳和微秒数。返回的字符串的格式:msec sec
语法
microtime(get_as_float)
参数 描述
get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。
说明
本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。
如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串,其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。字符串的两部分都是以秒为单位返回的。
代码:
<?php
echo microtime();echo '<br />';echo microtime(TRUE);
?>
浏览器显示结果:
0.45316500 12212282521221228252.45
我们看到microtime(TRUE),只返回了2位的小数。如果要输出更多位的微秒,需要函数number_format().看最后附1这个函数的用法。
补充:在PHP4下microtime()的用法 ,由于没有参数TRUE的用法,所以如果要得到浮点数,需要配合array_num()函数和explode()函数。
比如:
在PHP里面,要想统计一个action或者一个函数或者某个过程的执行所消耗的时间往往都只有一个办法:在运算前记录下时间戳,在运算后记录下时间戳,然后相减,就能得到一个相对比较实际的时间。 基本代码如下(从phpmyadmin里复制而来,懒得打了。这段代码其实也就是phpMyadmin里SQL的执行时间的计算):
PHP代码
<?php
// garvin: Measure query time.
// TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
$querytime_before = array_sum(explode(' ', microtime()));
$result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
$querytime_after = array_sum(explode(' ', microtime()));
$GLOBALS['querytime'] = $querytime_after - $querytime_before;
?>
看文章最后附2,array_sum()函数的用法。 其中 array_sum(explode(' ', microtime())); 是PHP4时代的写法,到PHP5之后,microtime函数多了一个bool值的参数,加上这个参数后可以直接得到 array_sum(explode(' ', microtime())); 相等的值,即:microtime(true)。 附1:number_format()的用法
number_format
格式化数字字串。 语法: string number_format(float number, int
相关文档:
zip.class.php
CODE:
[复制到剪切板]
<?
class
zip
{
var
$datasec
,
$ctrl_dir
= array();
var
$eof_ctrl_dir
=
"x50x4bx05x06x00x00x00x00"
;
var
$old_offset
=
0
; var
$dirs
......
have been studying parsing JSON from PHP using AJAX to display it in
the client side and jQuery had been a great help to me. Here is a very
simple code in parsing JSON using jQuery that i made.
tablejsondata.php
This file makes the request to a php file and displays the returned data into a tabl ......
<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="GBK" ?>';
echo '<users>';
echo '<user>';
echo '<name>'; echo '小小菜鸟';
echo '</name>';
echo '<age>';
echo '24';
  ......
在PHP开发中对比起Cookie,session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 session 的使用。
由于 Session 是以文本文件形式存储在服务器端的,所以不怕客户端修改 Session 内容。实际上在服务器端的 Session 文件,PHP 自动修改 session 文件的权限,只保留了系统读和 ......
用 PHP 控制浏览器缓存是非常容易的,手册上也相关的说明,由于很多初学者没有把手册看完,所以还是会有很多关于这个问题的疑问,故在此专门发一篇文章,同时对相关的语法做了详细的说明,方便新手查阅。
要解决这一问题,可以通过 PHP 中的 header() 函数,发送特定的缓存控制原始 HTTP 标头,具体代码如下:
Exampl ......