Home > Backend Development > PHP Tutorial > 关于简化代码。求大神指导

关于简化代码。求大神指导

WBOY
Release: 2016-06-23 14:38:45
Original
1047 people have browsed it

混迹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();	}
Copy after login
这要怎么写才简单呢?


回复讨论(解决方案)

这段代码的简化应在相关变量赋值时进行
你这是在做传入数据的验证,显然有形如
$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;}
Copy after login

哟西 楼上的跟我想法一样,我写不确定的if时候也是这么干的。有点像抽象的味道

哟西 楼上的跟我想法一样,我写不确定的if时候也是这么干的。有点像抽象的味道 呦西 大神你好

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template