发新话题
打印

注意php开发中的细节,寻找优化思路

注意php开发中的细节,寻找优化思路

<?php
// == 与 ===

$a = 0;
$b = NULL;
if ($a == $b) {
  //return true;

  print ("true!");
}
else {
  //return false;

  print ("wrong");
}
echo "<br />";
// empty 与 is_null

$str = "";
if (empty($str) ) {
  echo "true";
}
else {
  echo "not true";
}
echo "<br />";
if(is_null($str)) {
  echo "Null";
}
else {
  echo "NOT NULL";
}

// for while 与 foreach

$arr = array();
for($i = 0; $i < 50000; $i++){
$arr[] = $i*rand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
$time_start = GetRunTime();
for($i = 0; $i < count($arr); $i++){
$str .= $arr[$i];
}
$time_end = GetRunTime();
$time_used = $time_end - $time_start;
echo 'For:'.round($time_used, 7).'(s)<br><br>';
unset($str, $time_start, $time_end, $time_used);
$time_start = GetRunTime();
while(list($key, $val) = each($arr)){
$str .= $val;
}
$time_end = GetRunTime();
$time_used = $time_end - $time_start;
echo 'While:'.round($time_used, 7).'(s)<br><br>';
unset($str, $key, $val, $time_start, $time_end, $time_used);
$time_start = GetRunTime();
foreach($arr as $key => $val){
$str .= $val;
?>

TOP

发新话题