duangu1878 2015-02-11 08:57
浏览 18
已采纳

我遇到了在PHP中向函数返回false值的问题

This is a sample of individual functions that validate form data from a request submission. A variable of true has been set and each function checks for validation requirements then either continues without returning anything or returns false and changes the $check value. The function down the bottom then checks if the $check value has changed to false and if it has the SQL statement will not be run.

$check = true;

function productNameValidation(){

    if(isset($_REQUEST['product_name']) && !empty($_REQUEST['product_name']) && preg_match("/^[A-Za-z0-9 :]*[A-Za-z0-9][A-Za-z0-9 :]{0,50}$/",($_REQUEST['product_name']))){

        //then $valid['ID'] = "string: " . $_REQUEST['ID']
        $valid['product_name'] = $_REQUEST['product_name'];
        $err['product_name'] = "No errors";
        //if not    
    } else {
        if(empty($_REQUEST['product_name'])){
            $valid['product_name'] = "No data entered!";
        } else {    
            $valid['product_name'] = $_REQUEST['product_name'];
        } //$err['ID'] = "error message"

        $err['product_name'] = "Product Name must only contain letters, numbers and ':'!";
        $check = false; 
    }
}

function checkProduct()
{
    productNameValidation();
    productGenreValidation();
    productPriceValidation();
    productEsrbValidation();
    productThumbnailValidation();
    releaseDateValidation();

    return $check;
}
if($check == true)
{
   //Insert into database
}
  • 写回答

4条回答 默认 最新

  • drkjzk3359 2015-02-11 09:47
    关注

    What you need to do is add different variables on different functions. If you are working this code to the method that it begins as true and is required to be checked and if the check fails then becomes false, try this method:

       // $check = true;
    
        function productNameValidation(){
            $nameValidation = TRUE;
            if(isset($_REQUEST['product_name']) && !empty($_REQUEST['product_name']) && preg_match("/^[A-Za-z0-9 :]*[A-Za-z0-9][A-Za-z0-9 :]{0,50}$/",($_REQUEST['product_name']))){
    
                //then $valid['ID'] = "string: " . $_REQUEST['ID']
                $valid['product_name'] = $_REQUEST['product_name'];
                $err['product_name'] = "No errors";
                //if not    
            } else {
                if(empty($_REQUEST['product_name'])){
                    $valid['product_name'] = "No data entered!";
                } else {    
                    $valid['product_name'] = $_REQUEST['product_name'];
                } //$err['ID'] = "error message"
    
                $err['product_name'] = "Product Name must only contain letters, numbers and ':'!";
                $nameValidation = false; 
            }
        return $nameValidation;
        }
    
        function checkProduct()
        {
        $checkProduct = true; ///true until proven false.
    
            $checkProduct = productNameValidation();
            //This code gives $checkProduct the boolean value returned 
        //from the function
    
           $checkProduct = productGenreValidation();
            $checkProduct = productPriceValidation();
            $checkProduct = productEsrbValidation();
            $checkProduct = productThumbnailValidation();
            $checkProduct = releaseDateValidation();
    
            return $checkProduct;
        }
        if($checkProduct  == true)
        {
           //Insert into database
        } 
    

    What I have done here is each function returns a TRue/False flag boolean variables which can be checked with an if(){ statement, you can run through numerous functions in this way checking each aspect you need. The important point is that you need to return a value from each function and you can set the booleans manually with initial settings which is then updated upon conditionals - such as setting $checkProduct = TRUE until it is FALSE from any sub function.

    Global variables are really not a good idea in this case.

    Edit: Thanks to @Edward for some clarification of boolean return code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失