Home  >  Article  >  Backend Development  >  php代码如何简化

php代码如何简化

WBOY
WBOYOriginal
2016-06-13 12:27:241052browse

php代码怎么简化
怎么简化以下代码 用do while  实现只返回一次{"status":0,"error_code":0}' 就是说两个0是变量 有什么问题便返回那个代码。求实现代码
public function login(){
 
    $a_id =$_GET['a_id'];
    $a_password=$_GET['a_password'];

if(empty($a_id)) {
// $this->error('用户名不能为空');
echo '用户id不能为空{"status":0,"error_code":101}';
exit;
}
if(empty($a_password)) {
// $this->error('密码不能为空');
echo '密码不能为空 {"status":0,"error_code":101}';
exit;
}
// $password = md5($password);

$admin = M('Admin');
$where = "a_id='$a_id' and a_password='$a_password'";
//$count = $admin->where($where)->count();
$row = $admin->where($where)->find();
// var_dump($row);exit;
if($row['a_id']){
session('a_id',$row['$a_id']);
echo '{"status":1,"error_code":100}';
exit;
} else {
// $this->error('用户名或密码错误,请重新登陆');
// echo "登录失败!";
echo '{"status":0,"error_code":104}';
exit;
}
 }
------解决思路----------------------

public function login(){<br />  $res = array('status' => 0, 'error_code' => 100, 'msg' => '');<br />  $a_id =$_GET['a_id'];<br />  $a_password=$_GET['a_password'];<br />  while(1) {<br />    if(empty($a_id)) {<br />      $res['error_code'] = 101;<br />      $res['msg' = '用户名不能为空';<br />      break;<br />    }<br />    if(empty($a_password)) {<br />      $res['error_code'] = 101;<br />      $res['msg' = '密码不能为空';<br />      break;<br />    }<br />    $admin = M('Admin');<br />    $where = "a_id='$a_id' and a_password='$a_password'";<br />    $row = $admin->where($where)->find();<br />    if($row['a_id']){<br />      session('a_id',$row['$a_id']);<br />      $res['status' = 1;<br />      break;<br />    }<br />    $res['error_code'] = 104;<br />    $res['msg'] = ''用户名或密码错误,请重新登陆';<br />    break;<br />  }<br />  echo json($res);<br />}

Statement:
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