【问题标题】:Nested function inside While loop - PHP [duplicate]While循环内的嵌套函数-PHP [重复]
【发布时间】:2020-11-04 18:34:45
【问题描述】:

我正在尝试修复以下代码。在这段代码中,我创建了一个类,其中包含一个函数 testcal()。在该函数中,我连接到数据库,从表中进行选择查询。对于结果集,我想循环并获取字段 $field1、2、3 的值。使用这些字段,我正在创建另一个函数来抛出结果。

<?php
class test
{
    public function testcal()
    {
         set_error_handler(function($errno, $errstr){
            error_reporting(0);
        }, E_WARNING);
                
        //database credentials
        $host = "localhost";
        $db_name = "mydb";
        $username = "root";
        $password = "";
         
        //connect to database
        $con = mysqli_connect($host,$username,$password,$db_name);
        
        //Query to generate check-in variables from ffc_contrast
        $sql = "SELECT * FROM mytable";

        //Store result on $result variable
        $result = mysqli_query($con, $sql);
        
        while($row = mysqli_fetch_assoc($result)) 
        {
            
            //assign values to each variable
            $field1 = $row["field1"];
            $field2 = $row["field2"];
            $field3 = 0.5;
            
            ////////////////
            //Calculations//
            ////////////////
            
            function ratingcheck($field1,$field2){
                if($field1 == 0 || $field2 == 0){
                    return 0;
                } elseif ($field1 == 1) {
                    return 1;
                } elseif ($field2 == 2) {
                    return 2;
                }else{
                    return null;
                }
                }
        }
    }   
} 

$test = new test();
print $test->testcal();
?>

我收到一条错误消息,提示 Cannot redeclare ratingcheck() (previously declared in ~/myfile.php:51) in ~/myfile.php on line 37 我已正确声明该方法并已调用父方法。不知道为什么我会收到这个错误。有人可以帮忙吗?

【问题讨论】:

  • 在while循环里面,所以每次都要重新声明
  • 是的,我希望该函数在 while 循环中。思考如果存在某种逻辑是否有办法..
  • 是的,你需要在循环中调用它而不是定义它
  • 我是 PHP 新手,你能举个例子吗?

标签: php


【解决方案1】:
class test
{
    public function testcal()
    {
        ....all your code....

        //Store result on $result variable
        $result = mysqli_query($con, $sql);
        $returnValues = [];//not sure what you want here really, as you're returning inside youe while loop
        //so that will only do 1 iteration anyway, so i'm assuming you want to build an array or something
        
        
        while($row = mysqli_fetch_assoc($result))
        {

            //assign values to each variable
            $field1 = $row["field1"];
            $field2 = $row["field2"];
            $field3 = 0.5;


            //store the result from the function
            $returnValues[] = $this->ratingcheck($field1,$field2);

        }

        return $returnValues;
    }
    ////////////////
    //Calculations//
    ////////////////
    
    //declare the function once as part of the class, now you can call it
    //i've put private here as it's making an inernal calculation, make it public if you prefer
    private function ratingcheck($field1,$field2){
        if($field1 == 0 || $field2 == 0){
            return 0;
        } elseif ($field1 == 1) {
            return 1;
        } elseif ($field2 == 2) {
            return 2;
        }else{
            return null;
        }
    }
}

$test = new test();
print $test->testcal();

【讨论】:

  • 非常感谢。这有效:)
  • 顺便说一句,您的变量$field3 似乎是多余的
  • 是的,抱歉,这是我的其他逻辑的一部分。谢谢:)
【解决方案2】:

您将在 while 循环的每次迭代中重新创建函数 ratingcheck。您不能重新声明函数。

您需要将函数移到循环外,然后从循环内调用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多