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 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答