简体   繁体   English

如何使用标头函数返回定义的常量

[英]How to return defined constant with header function

I have my validation system and I want to make if something isn't ok it's return for example EMPTY but EMPTY is defined as function header. 我有验证系统,如果无法正常运行,我想返回它,例如EMPTY,但是EMPTY被定义为函数标头。

I tried something like below but it is used all the time, not only when name and pass fields are empty. 我尝试了以下类似方法,但它一直都在使用,不仅是在名称和密码字段为空的情况下。 I could just write if... { header...} but i don't want like that. 我可以写,如果... {header ...}但我不想那样。 How to make it? 怎么做?

include 'errors.php';
if(empty($name) || empty($pass){
      return EMPTY;
      exit();}
in another file: errors.php
define('EMPTY', header("Location: ../index.php?error=empty"))

it is used all the times because when you write 它一直被使用,因为当您写

define('EMPTY', header("Location: ../index.php?error=empty"));

the header function is run and its return value is assigned to the constant EMPTY. 头函数运行,并且其返回值分配给常量EMPTY。 If you want to have specifical functions running on errors and you want to define them in error.php, you should have 如果要使特定功能在错误上运行,并且要在error.php中定义它们,则应具有

//error.php
function got_error($cause){
    switch($cause){
         case 'EMPTY':
             header("location:../index.php?error=empty");
         break;
         case 'SHORT':
             header("location:../index.php?error=short");
         break;
         default:
              header("location:../index.php?error=unknown");
    }
   exit();
}

include 'errors.php';
if(empty($name) || empty($pass)){
    got_error('EMPTY');
}
if(strlen($pass) < 5){ //example of other error
    got_error('SHORT');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM