我試圖在基本的PHP forloop. 目前,我已經能夠執行多次并靜態編寫循環外的n-1代碼。除非我使用外部循環,n=n否則這個概念將失敗。有沒有辦法獲得循環內的最后一次執行?n = 1if-statement<?php $total_time_months = 6;$time_now = date('Y-m-d H:i:s');for ($x = 1; $x <= $total_time_months-1; $x++) { $offset = strtotime('+'. $x .' months'); echo $next_date = date('Y-m-d H:i:s', $offset)."<br>"; // this is the n-1 time}//do something statically knowing that there is suposed to be one more leftecho date('Y-m-d H:i:s', strtotime('+'. $total_time_months. ' months'))."<br>";?>對于這個問題,n = $total_time_months
2 回答

紫衣仙女
TA貢獻1839條經驗 獲得超15個贊
首先。當使用x <= y - 1
for 循環時,您始終可以將其簡化為“x < y”,而不是使用。
在循環內您可以檢查當前的$x
. 對于最后一項,它將是循環計數器的最大值 - 1。

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
您已經$x
在迭代中使用該變量。$x
只需檢查循環內的值:
if ($x === $total_time_months-1) { // ... do something }
- 2 回答
- 0 關注
- 170 瀏覽
添加回答
舉報
0/150
提交
取消