如果在同一页面中将功能传递给其他人

时间:2013-12-20 09:46:09

标签: php

我有类似的东西(我使用每个if用于AJAX的验证表格)

E.G:

 if($proc=="x"){

             ...some other proccses codes

             list($bol,$file) = returnImgBolean();
                 if($bol == true){
                  var_dump($file);
                  }

 }elseif($proc == "y"){

         ...some other proccses codes

        function returnImgBolean(){
            if(isset($msg) && !empty($msg)){
                     return array(true,$_FILES[$fileElementName]);
            }else{
                     @unlink($_FILES[$fileElementName]);    
                     return false;
            }
       }


 }

但它无法正常工作,我得到:Fatal error: Call to undefined function returnImgBolean() 如果在同一页面中我如何将该功能转移到其他人?

编辑1: 我试图拉出函数,但现在我得到了其他错误:

function returnImgBolean($msg,$file){
        if(isset($msg) && !empty($msg)){
        return array(true,$file);
        }else{
        @unlink($_FILES[$fileElementName]); 
        return false;
        }
    }

if($proc=="x"){

             ...some other proccses codes

             list($bol,$file) = returnImgBolean($msg,$file);
                 if($bol == true){
                  var_dump($file);
                  }

 }elseif($proc == "y"){

         ...some other proccses codes

  returnImgBolean($msg,$_FILES[$fileElementName]);


 }

现在错误是:

Notice: Undefined variable: msg

1 个答案:

答案 0 :(得分:3)

当您第一次致电returnImgBolean()时,它超出了范围。将它移出elseif块,如下所示:

function returnImgBolean() {
     if(isset($msg) && !empty($msg)) {
         return array(true,$_FILES[$fileElementName]);
     } else {
         @unlink($_FILES[$fileElementName]);    
         return false;
     }
}

if($proc=="x"){

    ...some other proccses codes

    list($bol,$file) = returnImgBolean();
    if( $bol == true ) {
         var_dump($file);
    }
 } elseif( $proc == "y" ) {

         ...some other proccses codes

 }