PHP功能效率问题

时间:2012-08-29 03:20:38

标签: php performance

以下代码的目的是检查日期和时间AM或PM的2个参数。我的代码有效,但我想提高效率并需要帮助。我有2个if语句,例如这个if语句sun pm和sun am,然后我有mon tues,wed,thur ......所以这很长。我被告知有一种方法可以缩短它,请帮忙。

if ($day == 'Sun' && $AMorPM == 'pm')
{
echo"
<fieldset>
<Legend><b>CHECK-OUT</b></Legend>
Date out:<input name='DateOut' type='text' value=$dateout disabled='disabled'><br>
Time out:<input name='TimeOut' type='text' value=$timeout disabled='disabled'> 
<input type='radio' name='AM'disabled='disabled'>AM 
<input type='radio' name='PM' checked disabled='disabled'>PM<br>
Day of week: <input type='radio' name='S'checked disabled='disabled'>S 
<input type='radio' name='Mon' disabled='disabled'>M 
<input type='radio' name='Tue'disabled='disabled'>T 
<input type='radio' name='Wed'disabled='disabled'>W 
<input type='radio' name='Thu'disabled='disabled'>R 
<input type='radio' name='Fri'disabled='disabled'>F 
<input type='radio' name='Sat'disabled='disabled'>S
</fieldset>";
}

2 个答案:

答案 0 :(得分:0)

你是否对AM和PM做同样的事情而不管一周中哪一天?不管一天中的哪个时间,你每周的每一天都做同样的事吗?如果是这样,那你为什么不先分别处理AM / PM位和一周中的哪一天呢?

echo "<fieldset>
    <Legend><b>CHECK-OUT</b></Legend>
    Date out:<input name='DateOut' type='text' value=$dateout disabled='disabled'><br>
    Time out:<input name='TimeOut' type='text' value=$timeout disabled='disabled'> 
    <input type='radio' name='AM'";

if ($AMorPM == "am")
   echo " checked ";

echo "disabled='disabled'>AM <input type='radio' name='PM'";

if ($AMorPM == "pm")
    echo " checked ";

echo " disabled='disabled'>PM<br>";

无论选择哪一天,该代码都会在一天中的某个时间执行您想要的操作。现在对一周中的每一天做同样的事情。

答案 1 :(得分:0)

您可以将其设置为函数 -

//create array of days/short day codes
$day_array = array('Sun'=>'S', 'Mon'=>'M', 'Tue'=>'T', 'Wed'=>'W', 'Thu'=>'R', 'Fri'=>'F', 'Sat'=>'S');

//creates clock function
function clock($today, $ampm, $date, $time, $dayarray){

if ($ampm == 'am') {$punch_type =  "CHECK-IN";
                   $inout = 'in'; 
                   $inout_up = ucfirst($inout);
                   $checked_am = 'checked = "checked"';
} 
else               {$punch_type = "CHECK-OUT";
                   $inout = 'out'; 
                   $inout_up = ucfirst($inout);
                   $checked_pm = 'checked = "checked"';
}
//sets day of the week array vars, based of today
$dayof_week = $dayarray[$today];

echo"
<fieldset>
<Legend><b>$punch_type</b></Legend>
Date $inout:<input name='Date$inout_up' type='text' value=$date disabled='disabled'><br>
Time $inout:<input name='Time$inout_up' type='text' value=$time disabled='disabled'> 
<input type='radio' name='AM' $checked_am disabled='disabled'>AM <input type='radio' name='PM' $checked_pm disabled='disabled'>PM<br>
Day of week: ";

// loop through day array to create radios for every day of the week
foreach($dayarray as $key=>$value){
    // sets which day is checked
    $chkd = ($today == $key) ? ' checked = "checked"' : ''; 
    // creates each radio
    echo "<input type='radio' name='$key' $chkd disabled='disabled'>$value";}

echo"</fieldset>";
}

然后您可以通过执行 -

来调用该函数
clock("Sun", "am", "8/28/12", "10:30", $day_array);

这样,您不需要每天都重写脚本。如果您只是在函数中动态设置4个变量,它将动态构建/显示您的时钟 即

clock($day, $AMorPM, $date, $time, $day_array);