if (1 == $orderState) {
$orderTitle = '已预定';
} else {
$orderTitle = '已售完';
}
// 优化后
$orderTitle = '已售完';
if (1 == $orderState) {
$orderTitle = '已预定';
}
if (strlen($newPwd) < 6) {
$message = '密码长度不足!';
}
strlen($newPwd) < 6 && $message = '密码长度不足!';
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}
//优化后
$action = empty($_POST['action']) ? 'default' : $_POST['action'];
$action = empty($_POST['action']) ? 'default' : $_POST['action'];
// 简写后
echo $action = $_POST['action'] ?:'default' ;
//必须保证action必须是存在的,否则Notice: Undefined index: a xxx
function isLeapYear($year) {
if (($year % 4 == 0 && 100 !=0) || ($year % 400 == 0)) {
return true;
} else {
return false;
}
}
//优化后
function isLeapYear($year) {
return ($year % 4 == 0 && 100 !=0) || ($year % 400 == 0);
}
$('#selStatus').bind('change',function(){
$(this).val() == 'disabled' ? $('.reason').removeClass('hidden') : $('.reason').addClass('hidden');
});
if ('玄幻' == $sortname) {
$sort = 1;
} else if ('武侠' == $sortname) {
$sort = 2;
} else if ('言情' == $sortname) {
$sort = 3;
} else if ('其他' == $sortname) {
$sort = 10;
}
//优化后
switch ($sortname) {
case '玄幻':
$sort = 1;
break;
case '武侠':
$sort = 2;
break;
case '言情':
$sort = 3;
break;
case '其他':
$sort = 10;
break;
}
$sortTable = array(
'玄幻' => 1,
'武侠' => 2,
'言情' => 3,
'其他' => 10,
);
$sortId = $sortTable[$sortname];
优化10:避免使用幻数(magic numbers)
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
//优化后
define('APP_CHARSET', 'UTF-8');
<meta http-equiv="Content-Type" content="text/html;charset={APP_CHARSET}" />
$str = 'this_is_a_test';
$humpstr = implode('', array_map('ucfirst', explode('_', $str)));
// 优化后
$str = $str = 'this_is_a_test';
$words = explode('_', $str);
$uWords = array_map('ucfirst', $words);
$humpstr = implode('', $uWords);
if (!$hasone && $ddisfirst = 1 && $litpic == '' && empty($litpicname)) {
$litpcname = GetImageMapDD($iurl, $cfg_ddimg_width);;
}
// 优化后
$emptyPic = ($litpic == '' && empty($litpicname));
$validFirstPic = (!$hasone && $ddisfirst);
if ($validFirstPic && $emptyPic) {
$litpcname = GetImageMapDD($iurl, $cfg_ddimg_width);;
}