關於簡單化代碼。求大神指導

來源:互聯網
上載者:User
關於簡化代碼。求大神指導
混跡csdn有半個月了。收穫不少,想在這裡安家了。
喜歡這裡的氛圍,喜歡這裡的大神。
if(empty($grouptypeid)){
ShowMsg('請指定所屬考試!', '-1');
exit();
}if(empty($content)){
ShowMsg('內容不可為空!','-1');
exit();
}if(empty($teachername)){
ShowMsg('請選擇教師!','-1');
exit();
}if(empty($years)){
ShowMsg('請選擇日期','-1');
exit();
}if(empty($isupdate)){
ShowMsg('請選擇更新日期','-1');
exit();
}
這要怎麼寫才簡單呢?

分享到: 更多


------解決方案--------------------
這段代碼的簡化應在相關變數賦值時進行
你這是在做傳入資料的驗證,顯然有形如
$grouptypeid = $_POST['grouptypeid'];
這樣的程式碼片段

於是有
$dict = array(
'grouptypeid' => '請指定所屬考試!',
'content' => '內容不可為空!',
'teachername' => '請選擇教師!',
'years' => '請選擇日期',
'isupdate' => '請選擇更新日期',
);
foreach($_POST as $k=>$v) {
if(empty($v)) {
ShowMsg($dict[$k], '-1');
exit;
}
$$k = $v;
}

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.