【问题标题】:Count how many times functions echo a statement计算函数回显语句的次数
【发布时间】:2011-03-07 12:42:24
【问题描述】:

我有一个调用另外两个函数的函数:

class myClass{


    function myFunc(){

        for($i=0;$i<500;$i++){
            $this->func1();
            $this->func2();
        }
    }

    function func1(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true)
             echo "This is from func1, and may echo 0-1000 times";
    }

    function func2(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true)
             echo "This is from func2, and may echo 0-1000 times";
    }
}

我想做的是找出一种方法,我可以获取函数回显某些内容的总时间,并将该信息显示在 myFunc() 中。我写了一个计数函数,但它并没有像我预期的那样工作。

有什么建议吗?

【问题讨论】:

  • 你应该编辑你的原始问题而不是删除它,特别是因为很多人花时间写答案。
  • $something 真的改变了吗?否则你有一个无限循环。
  • @webbiedave:我很抱歉,我在意识到有回应之前就删除了它。显然你太快了。
  • @felix: $something 在某些时候总是会被评估为假。

标签: php function counting


【解决方案1】:

这是一种方法:

class myClass{
    private $count;

    function myFunc(){

        for($i=0;$i<500;$i++){
            $this->func1();
            $this->func2();
        }
    }

    function func1(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             $this->count++;
             echo "This is from func1, and may echo 0-1000 times";
         }
    }

    function func2(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             $this->count++;
             echo "This is from func2, and may echo 0-1000 times";
         }
    }
}

或者更好的方法:

class myClass{
    private $count;

    function myFunc(){

        for($i=0;$i<500;$i++){
            $this->func1();
            $this->func2();
        }
    }

    function func1(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             echoMe("This is from func1, and may echo 0-1000 times");
         }
    }

    function func2(){
         // Does some stuff
         // echos statements, and can vary the amount of echoed statements
         while($something == true) {
             echoMe("This is from func2, and may echo 0-1000 times");
         }
    }

    function echoMe($msg) {
        echo $msg;
        $this->count++;
    }
}

【讨论】:

  • 你必须从这两个函数中删除$this-&gt;echo++
【解决方案2】:
function myFunc(){
    $echo_count = 0;
    for($i=0;$i<500;$i++){
        $echo_count += $this->func1();
        $echo_count += $this->func2();
    }
    echo $echo_count;
}

function func1(){
     // Does some stuff
     // echos statements, and can vary the amount of echoed statements
     $count = 0;
     while($something == true){
         echo "This is from func1, and may echo 0-1000 times";
         $count++;
     }
     return $count;
}

function func2(){
     // Does some stuff
     // echos statements, and can vary the amount of echoed statements
     $count = 0;
     while($something == true){
         echo "This is from func2, and may echo 0-1000 times";
         $count++;
     }
     return $count;
}

【讨论】:

    【解决方案3】:

    为什么不让函数返回回声计数:

    $echoCount = 0;
    while ($something == true) {
        echo "This is from func1, and may echo 0-1000 times";
        $echoCount++;
    }
    
    return $echoCount;
    

    然后在 myFunc 中,你可以累积它们:

    function myFunc() {
    
        $totalEchoes = 0;
        for ($i=0; $i<500; $i++) {
            $totalEchoes += $this->func1() + $this->func2();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2014-06-17
      • 1970-01-01
      • 2018-12-29
      相关资源
      最近更新 更多