确定了PHP脚本所需的时间执行正确的一微秒。
插入在页面顶部的代码:
<?php
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
?>
然后添加在页面底部的以下内容:
<?php
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo 'This page was created in ' .$totaltime. ' seconds.';
?>
PHP的microtime中()函数返回当前的Unix时间戳微秒。
此脚本时,页面最后修订和产出作为二十分钟前,或XX天前的日期...或者即使您不更新太多 - 二十周前显示!
//File Name
$last_modified = filemtime("FILE.php");
{
$timediff = time() - $last_modified;
if ($timediff < 3600)
{
if ($timediff < 120)
{
$returndate = "1 minute ago.";
}
else
{
$returndate = intval($timediff / 60) . " minutes ago.";
}
}
else if ($timediff < 7200)
{
$returndate = "1 hour ago.";
}
else if ($timediff < 86400)
{
$returndate = intval($timediff / 3600) . " hours ago.";
}
else if ($timediff < 172800)
{
$returndate = "1 day ago.";
}
else if ($timediff < 604800)
{
$returndate = intval($timediff / 86400) . " days ago.";
}
else if ($timediff < 1209600)
{
$returndate = "1 week ago.";
}
else if ($timediff < 3024000)
{
$returndate = intval($timediff / 604900) . " weeks ago.";
}
else
{
$returndate = @date('n-j-Y', $timestamp);
if($type=="fulldate")
{
$returndate = @date('n-j-y, H:i', $timestamp);
}
else if ($type=="time")
{
$returndate = @date('H:i', $timestamp);
}
}
//Display It
print("Last Modified: ");
print($returndate);
}
?>
它应该很容易理解。
$ last_modified = filemtime(“FILE.php”); - 更改FILE.php到文件您要连接。
有时,您可能希望让无条件循环的开始,并允许括号内的语句来决定何时退出循环。
有两个特殊的语句可用在循环使用:中断和继续。
break语句终止或For循环的同时,继续执行现行的代码如下循环后(如有)。或者,
你可以把一个数字后,折价关键字,说明如何循环结构的多层次,以摆脱。这样,埋
藏在一份声明中深层嵌套的循环可以打破最外层循环。
<?php
echo "<p><b>Example of using the Break statement:</b></p>";
for ($i=0; $i<=10; $i++) {
if ($i==3){break;}
echo "The number is ".$i;
echo "<br />";
}
echo "<p><b>One more example of using the Break statement:</b><p>";
$i = 0;
$j = 0;
while ($i < 10) {
while ($j < 10) {
if ($j == 5) {break 2;} // breaks out of two while loops教程
$j++;
}
$i++;
}
echo "The first number is ".$i."<br />";
echo "The second number is ".$j."<br />";
?>
continue语句终止了在语句块执行一或For循环的同时,继续对下一个迭代循环的执行
:
<?php
echo "<p><b>Example of using the Continue statement:</b><p>";
for ($i=0; $i<=10; $i++) {
if (i==3){continue;}
echo "The number is ".$i;
echo "<br />";
}
?>
foreach循环是一种变异的For循环,并允许您遍历数组中的元素。有两个不同版本的
foreach循环。 foreach循环的语法如下:
foreach (array as value)
{
code to be executed;
}
foreach (array as key => value)
{
code to be executed;
}
下面的例子演示了foreach循环,将打印给定数组的值:
<?php
$email = array('john.smith@example.com', 'alex@example.com');
foreach ($email as $value) {
echo "Processing ".$value."<br />";
}
?>
PHP的执行每个元素美元,反过来电子邮件循环体一次,美元值设置为当前元素。元素
处理其国内秩序。循环继续,直到达到foreach循环的最后一个元素或上给定数组的约
束。
<?php
$person = array('name' => 'Andrew', 'age' => 21, 'address' => '77, Lincoln
st.');
foreach ($person as $key => $value) {
echo $key." is ".$value."<br />";
}
?>
在这种情况下,对每个元素的关键是放置在$键和相应的值是$的重视。
在foreach构造不营业数组本身,而是它的一个副本。在每次循环中,变量$值的值可
以操作,但该数组的原始值保持不变。
在做... while语句类似的声明时,除了测试条件在每次迭代结束,而不是在开始。这
意味着你... While循环是保证至少运行一次。在做... While循环的语法如下:
do
{
code to be exected;
}
while (condition);
下面将增加i的值至少一次的例子,它将继续递增,只要它有一个变量i的值小于或等
于10:
<?php教程
$i = 0;
do {
echo "The number is ".$i."<br/>";
$i++;
}
while ($i <= 10);
?>