PHP goto特性使用及心得与本周与其他周日期获取
今天终于在项目中用到了goto特性
foreach ($coursewares as $key => $courseware) {
st:
$this_time = $new_times_tmp->where('c', $c)->first();
if (!$this_time) {
$c = $c + 1;
$this_time = $new_times_tmp->where('c', $c)->first();
}
$new_times_tmp->where('c', $c)->first()->c = $this_time->c + 1;
$day = $this->num_to_week($this_time->week);
if($c==0){
$begin_date = date('Y-m-d',strtotime('last '.$day));
}else{
$begin_date = date('Y-m-d',strtotime('+'.$c.' week last '.$day));
}
if ($festivals->where('festival_date', $begin_date)->first()) {
goto st;
}
$new_cous->push([
'cou_id' => $courseware->id,
'date' => $begin_date,
'begin_time' => $time_collect[$key]->begin_time,
'end_time' => $time_collect[$key]->end_time,
]);
}
st:是声明一个作用域(只能在一个函数或类方法中使用),可以理解为标记,如果需要跳转到某一作用域时可以 用goto跳转
本来php作为解释语言是从上到下解释执行,有了goto就可以从下到上 从任意位置跳任意位置执行, 实在太方便了.
- 上一篇: php获取本周日期和下周日期
- 下一篇: 如何检测MySQL中建立的索引是否生效