2016-11-24 22 views
-1

我想在我的for循環中使用static關鍵字。在for循環中使用static關鍵字?

可能嗎?如果是,那麼如何?

這裏是我的代碼:

$current_time = date('h:i A'); 
    <select> 

     for($i = 0; $i <= 5; $i++) { 
      if ($i=0) { 
       echo "<option>" . date("h:i A", $current_time) . "</option>"; 
      }else{ 
       static $tNow = strtotime("+15 minutes",strtotime($current_time)); 
       echo "<option>" . date("h:i A", $tNow) . "</option>"; 
      } 
     } 
    <select> 

當我使用static關鍵字,我發現了一個PHP錯誤。

我想顯示一個<select>元素,每個選項爲15分鐘步驟,如12:0012:15,12:30

+1

**你究竟是想通過這個**達到**? –

+1

但是,爲什麼你需要使用它? – malutki5200

+0

通過使用** static **關鍵字,您的目標是什麼?你有沒有閱讀何時/在哪些情況下我們可以使用這個http://php.net/manual/en/language.oop5.static.php? –

回答

0

我不知道爲什麼你要在你的代碼中使用static keyword,但建議你應該閱讀這裏:http://php.net/manual/en/language.oop5.static.php,如何以及何時使用static keyword
如果你的目標是要顯示下拉列表,並獲得期權價值不同15mint時間 這樣的12:00,12:15,12:30 .....這是我的解決方案:

$current_time[0] = date('h:i A'); 
echo"<select>"; 
echo "<option>" . $current_time[0] . "</option>"; 
$step=0; 
for($i = 1; $i <=5; $i++) { 
    $step=$step+15; 
    $current_time[$i] = strtotime("+".$step." minutes", strtotime($current_time[0])); 
    echo "<option>" . date("h:i A", $current_time[$i]) . "</option>"; 
} 

echo"</select>"; 
var_dump($current_time); 

工作代碼這裏:http://ideone.com/zM0eue

說明:我用一個數組來包含for loop中的所有strtotime輸出。每次我使用相同的$current_time[0]來計算新的$current_time[$i]$step變量每次增加+15。

+0

非常感謝的人..... –

+0

不客氣! – Yeti82