Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更有效的方法,而不是使用重复的if语句_Php - Fatal编程技术网

Php 更有效的方法,而不是使用重复的if语句

Php 更有效的方法,而不是使用重复的if语句,php,Php,我有以下代码: if (strtolower($_POST['skype']) == "yummy") echo "<pre>".file_get_contents("./.htfullapps.txt")."</pre>"; elseif ($_POST['skype'] == '' or $_POST['IGN'] == '' or $_POST['pass'] == '' or !isset($_POST

我有以下代码:

if (strtolower($_POST['skype']) == "yummy") 
   echo "<pre>".file_get_contents("./.htfullapps.txt")."</pre>";
elseif ($_POST['skype'] == '' or 
        $_POST['IGN'] == '' or 
        $_POST['pass'] == '' or 
        !isset($_POST['rules']) or 
        !isset($_POST['group']) or 
        strlen($_POST['pass']) <= 7)
{
    redir( "http://ftb.chipperyman.com/apply/?fail&error=one%20or%20more%20fields%20did%20not%20meet%20the%20minimum%20requirements" ); //Redir is a function defined above and works fine.
    exit;
}
if(strtolower($_POST['skype'])==“好吃”)
echo“.file_get_contents(“./.htfullapps.txt”);
elseif($_POST['skype']==''或
$\u POST['IGN']==''或
$\u POST['pass']==''或
!isset($_POST['rules'])或
!isset($_POST['group'])或
strlen($_POST['pass'])怎么样

class Request {
    protected $items = array(
                             'get' => array(),
                             'post' => array()
                       );
    public function __construct(){
        $this->items['post'] = $_POST;
        $this->items['get'] = $_GET;
    }

    public function isPost(){
        return ($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false;
    }

    public function isGet(){
       return ($_SERVER['REQUEST_METHOD'] == 'GET') ? true : false;
    }

    public function getPost($name){
        return (isset($this->items['post'][$name])) ? $this->items['post'][$name] : null; 
    }

    public function get($name){
       return (isset($this->items['get'][$name])) ? $this->items['get'][$name] : null;
    }
}
$field\u min\u len=array('skype'=>1,'IGN'=>1,'pass'=>7);
对于($field\u min\u len as$f=>l){
如果(!isset($_POST[$f])| | strlen($_POST[$f])<$l){
redir(…);
出口
}
}
怎么样

class Request {
    protected $items = array(
                             'get' => array(),
                             'post' => array()
                       );
    public function __construct(){
        $this->items['post'] = $_POST;
        $this->items['get'] = $_GET;
    }

    public function isPost(){
        return ($_SERVER['REQUEST_METHOD'] == 'POST') ? true : false;
    }

    public function isGet(){
       return ($_SERVER['REQUEST_METHOD'] == 'GET') ? true : false;
    }

    public function getPost($name){
        return (isset($this->items['post'][$name])) ? $this->items['post'][$name] : null; 
    }

    public function get($name){
       return (isset($this->items['get'][$name])) ? $this->items['get'][$name] : null;
    }
}
$field\u min\u len=array('skype'=>1,'IGN'=>1,'pass'=>7);
对于($field\u min\u len as$f=>l){
如果(!isset($_POST[$f])| | strlen($_POST[$f])<$l){
redir(…);
出口
}
}

创建用于解析post和get数据的请求类,该类帮助您验证未定义的空字段,并创建报告类帮助您抛出错误

下面是一个非常简单的请求类:

Class Report {
    protected static $instance;
    private $messages = array();

    private function __construct(){}

    public function getInstance(){
        if(!self::$instance){
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function addReport($message){
        $this->messages[] = $message;
    }

    public function hasReports(){
        return (!empty($this->messages)) ? true : false;
    }
    public function getReports(){
       return $this->messages;
    }

    //this is not so cleaned .... it must be in template but for example
    public function throwReports(){
        if(!empty($this->messages)){
           foreach($this->messages as $message){
               echo $message."<br />";
           }
        }
    }
}
报告课:

$request = new Request();
$report = Report::getInstance();
if($request->isPost())
{
  if(!$request->getPost("icq")){
    $report->addMessage("you dont enter ICQ");
  }

  if(!$request->getPost("skype")){
    $report->addMessage("you dont enter SKYPE");
  }
  //....etc

  //if we have some reports throw it.
  if($report->hasReports()){
      $reports->throwReports();
  }
}

您可以将报表类与会话组合在一起,并在重定向后抛出错误,只需将该类更新为将报表保存到会话而不是$messages,并且在重定向后,如果您希望让消息同时抛出并清除该类。

创建用于解析post和get数据的请求类,该类可帮助您验证未定义d、 空字段和报告类,帮助您抛出错误

下面是一个非常简单的请求类:

Class Report {
    protected static $instance;
    private $messages = array();

    private function __construct(){}

    public function getInstance(){
        if(!self::$instance){
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function addReport($message){
        $this->messages[] = $message;
    }

    public function hasReports(){
        return (!empty($this->messages)) ? true : false;
    }
    public function getReports(){
       return $this->messages;
    }

    //this is not so cleaned .... it must be in template but for example
    public function throwReports(){
        if(!empty($this->messages)){
           foreach($this->messages as $message){
               echo $message."<br />";
           }
        }
    }
}
报告课:

$request = new Request();
$report = Report::getInstance();
if($request->isPost())
{
  if(!$request->getPost("icq")){
    $report->addMessage("you dont enter ICQ");
  }

  if(!$request->getPost("skype")){
    $report->addMessage("you dont enter SKYPE");
  }
  //....etc

  //if we have some reports throw it.
  if($report->hasReports()){
      $reports->throwReports();
  }
}

您可以将报表类与会话组合,并在重定向后抛出错误,只需将该类更新为将报表保存到会话而不是$messages,并且在重定向后,如果您想让消息同时抛出并清除它。

您可以像这样使用关联数组

// validation parameters
$validation = array(
   'skype' => array('check' => 'not_empty', 'error' => 'skype empty'),
   'IGN'   => array('check' => 'not_empty', 'error' => 'IGN empty'),
   'pass'  => array('check' => 'size', 'params' => array(7), 'error' => 'invalid password'),
   'group' => array('check' => 'set', 'error' => 'group unset'),
   'rules' => array('check' => 'set', 'error' => 'group unset')
);

// validation class
class Validator {

  private $params;
  private $check_methods = array('not_empty', 'size', 'set');

  public function __construct($params){
    $this->params = $params;
  }

  private function not_empty($array, $key){
    return $array[$key] == '';
  }

  private function size($array, $key ,$s){
    return strlen($array[$key]) < $s;
  }

  private function set($array, $key){
    return isset($array[$key]);
  }

  private handle_error($err, $msg){
    if ($err) {
       // log, redirect etc.
    }
  }

  public function validate($data){

    foreach($params as $key => $value){
      if (in_array($value['check'], $this->check_methods)){
        $params = $value['params'];
        array_unshift($params, $data, $key);
        $this->handler_error(call_user_func_array(array($this,$value['check']),
                                                  $params),
                             $value['error']);
      }
    }
  }
};


// usage
$validator = new Validator($validation);
$validator->validate($_POST);
功能重拨($var){
echo$var;
}
$skypeErr=数组(“”=>)http://ftb.chipperyman.com/apply/?fail&error=your%20skype%20is%20invalid%20because%20it%20is%20empty");
$IGNErr=数组('''=>'err2');
$passer=array(''=>'err3',True:'err4');
重拨($skypeErr[$\u POST['skype']]);
重拨($IGNErr[$\u POST['IGN']]);
redir($passer[$_POST['pass']]);

redir($passer[strlen($\u POST['pass'])您可以像这样使用关联数组

// validation parameters
$validation = array(
   'skype' => array('check' => 'not_empty', 'error' => 'skype empty'),
   'IGN'   => array('check' => 'not_empty', 'error' => 'IGN empty'),
   'pass'  => array('check' => 'size', 'params' => array(7), 'error' => 'invalid password'),
   'group' => array('check' => 'set', 'error' => 'group unset'),
   'rules' => array('check' => 'set', 'error' => 'group unset')
);

// validation class
class Validator {

  private $params;
  private $check_methods = array('not_empty', 'size', 'set');

  public function __construct($params){
    $this->params = $params;
  }

  private function not_empty($array, $key){
    return $array[$key] == '';
  }

  private function size($array, $key ,$s){
    return strlen($array[$key]) < $s;
  }

  private function set($array, $key){
    return isset($array[$key]);
  }

  private handle_error($err, $msg){
    if ($err) {
       // log, redirect etc.
    }
  }

  public function validate($data){

    foreach($params as $key => $value){
      if (in_array($value['check'], $this->check_methods)){
        $params = $value['params'];
        array_unshift($params, $data, $key);
        $this->handler_error(call_user_func_array(array($this,$value['check']),
                                                  $params),
                             $value['error']);
      }
    }
  }
};


// usage
$validator = new Validator($validation);
$validator->validate($_POST);
功能重拨($var){
echo$var;
}
$skypeErr=数组(“”=>)http://ftb.chipperyman.com/apply/?fail&error=your%20skype%20is%20invalid%20because%20it%20is%20empty");
$IGNErr=数组('''=>'err2');
$passer=array(''=>'err3',True:'err4');
重拨($skypeErr[$\u POST['skype']]);
重拨($IGNErr[$\u POST['IGN']]);
redir($passer[$_POST['pass']]);

redir($passer[strlen($_POST['pass'])可能是类似的(可重复使用,但很长):

//验证参数
$validation=array(
“skype”=>array('check'=>“not_empty”,“error'=>“skype empty”),
'IGN'=>array('check'=>'not_empty','error'=>'IGN empty'),
'pass'=>array('check'=>'size','params'=>array(7),'error'=>'invalid password'),
'group'=>array('check'=>'set','error'=>'group unset'),
'规则'=>数组('检查'=>'设置','错误'=>'组未设置')
);
//验证类
类验证器{
私人$params;
私有$check_methods=array('not_empty','size','set');
公共函数构造($params){
$this->params=$params;
}
私有函数不为空($array,$key){
返回$array[$key]=='';
}
私有函数大小($array,$key,$s){
返回strlen($array[$key])<$s;
}
私有函数集($array,$key){
返回isset($array[$key]);
}
私有句柄\u错误($err,$msg){
如果($err){
//日志、重定向等。
}
}
公共功能验证($data){
foreach($key=>$value的参数){
if(在数组中($value['check'],$this->check\u方法)){
$params=$value['params'];
数组_unshift($params,$data,$key);
$this->handler\u错误(调用\u user\u func\u数组(数组($this,$value['check')),
$params),
$value['error']);
}
}
}
};
//用法
$validator=新的验证器($validation);
$validator->validate($\u POST);
只需使用新的检查、特殊的日志函数等扩展类


警告:未经测试的代码。

可能类似(可重复使用,但冗长):

//验证参数
$validation=array(
“skype”=>array('check'=>“not_empty”,“error'=>“skype empty”),
'IGN'=>array('check'=>'not_empty','error'=>'IGN empty'),
'pass'=>array('check'=>'size','params'=>array(7),'error'=>'invalid password'),
'group'=>array('check'=>'set','error'=>'group unset'),
'规则'=>数组('检查'=>'设置','错误'=>'组未设置')
);
//验证类
类验证器{
私人$params;
私有$check_methods=array('not_empty','size','set');
公共函数构造($params){
$this->params=$params;
}
私有函数不为空($array,$key){
返回$array[$key]=='';
}
私有函数大小($array,$key,$s){
返回strlen($array[$key])<$s;
}
私有函数集($array,$key){
返回isset($array[$key]);
}
私有句柄\u错误($err,$msg){
如果($err){
//日志、重定向等。
}
}
公共功能验证($data){
foreach($key=>$value的参数){
if(在数组中($value['check'],$this->check\u方法)){
$params=$value['params'];
数组_unshift($params,$data,$key);
$this->handler\u错误(调用\u user\u func\u数组(数组($this,$value['check')),
$params),
$value['error']);
}
}
}
};
//用法
$validator=新的验证器($validation);
$validator->validate($\u POST);
只需使用新的检查、特殊的日志函数等扩展类


警告:未经测试的代码。

我现在就是这样做错误报告的:


以下是我现在执行错误报告的方式:

绕五圈