多个OR表达式在PHP中获取错误

时间:2016-03-18 09:27:17

标签: php

我有一个表单有多个提交。 表单1:$ action =“constregulation_add”和 表格2:$ action =“constregulation_edit” 不知何故,当我提交时,此操作总是在下面的代码中以2验证结束。我的问题是,当我尝试以表格2提交时,不应该只是在条件2中结束(不包括在条件1中)? 但是,当我运行此代码时,为什么此代码也在条件1中运行?

条件1:

if ($action == "procedure_add" or $action == "projectplan_add" or $action == "work_add" or $action == "form_add" 
        or $action == "template_add" or  $action == "imsdoc_add" or $action == "safety_add" or $action=="projectprogress_add"  
        or $action=="lessondetail_add" or  $action=="regulation_add" or $action="constregulation_add"){ 


        if (isset($_FILES['document'])) {
            $doc=$_FILES['document'];
            $extensionList = array("doc", "docx", "pdf","xls","xlsx","jpg","jpeg");
            $fileName = $_FILES['document']['name'];
            $pecah = explode(".", $fileName);
            $ekstensi = $pecah[1];

            if (!in_array($ekstensi, $extensionList)){
                $error[] = "<h5>* You're file not PDF or Word or Excel or Image JPG files</h5>";
            }
        }
        else {

            $error[] = "<h5> * File can not empty</h5>";
        }   
    }

条件2:

if($action == "procedure_edit" or $action == "work_edit" or $action == "form_edit" or $action == "template_edit" 
        or $action == "projectplan_edit" or $action == "safety_edit" or $action == "projectprogress_edit" or $action=="regulation_edit"
        or $action =="constregulation_edit"){   

        if (isset($_FILES['document'])) {
            $doc=$_FILES['document'];
            $extensionList = array("doc", "docx", "pdf","xls","xlsx","jpg","jpeg");
            $fileName = $_FILES['document']['name'];
            $pecah = explode(".", $fileName);
            $ekstensi = $pecah[1];
            $param="ada";
            if (!in_array($ekstensi, $extensionList)){
                $error[] = "<h5>* You're file not PDF or Word or Excel or Image JPG files</h5>";
            }

        } else {

            $param="tidak ada";

        }

    }

1 个答案:

答案 0 :(得分:2)

这是因为在第一个条件上最后一次检查你只有一个等号。你应该有两个等于检查。此处(...) or $action="constregulation_add" (...)更改为(...) or $action == "constregulation_add" (...) 如果您只使用一个,则始终为true,因为值constregulation_add被复制到$action。我认为这是一个错字。