简体   繁体   English

PHP for循环仅运行一次

[英]PHP for loop only runs once

I've been taking the php course from code academy. 我一直在从代码学院上PHP课程。 In one of the exercises, i've tried to implement a conditional function, where it would take a parameter for how many times the function would run. 在其中一个练习中,我尝试实现一个条件函数,该函数需要一个参数来说明函数将运行多少次。 Unfortunately it is not running. 不幸的是它没有运行。 I'm trying to run the bark() twice with bark(2). 我试图用bark(2)运行两次bark()。 It's only echoing once. 只回声一次。 I have tried a "do while", "if" and "for". 我尝试了“做一会儿”,“如果”和“为”。 None worked. 没有工作。 Why is that? 这是为什么?

<?php
   class Dog{
        public $numLegs = 4;
        public $name;
        public $speak = "Woof!";
  public function bark($up){
      $counter = 0;
      for($counter; $counter!=$up;$counter++){
      return $this->speak;
      }
  }

  public function greet(){
      return "Hello " . $this->name . "!" . "<br />";
  }

   public function __construct($name){
       $this->name = $name;
    }
   }
   $dog1 = new Dog("Barker");
   $dog2 = new Dog("Amigo");

   echo $dog1 -> bark(2);
   echo $dog2 -> name;
?>

You are returning $this->speak , as soon as you return a value the entire function stops execution. 您将返回$this->speak ,一旦返回值,整个函数将停止执行。

You could do something like: 您可以执行以下操作:

public function bark($up){
    $counter = 0;
    $return = '';
    for($counter; $counter<$up;$counter++){
        $return .= $this->speak;
    }

    return $return;
}

To add another solution, without using for at all using array_fill() to create an array of the wanted size and content (since it is always the same text) and then simply implode() to receive a String as result. 要添加其他的解决方案,而无需使用for所有使用array_fill()创建想要的大小和内容的数组(因为它始终是相同的文字),然后简单地implode()接收一个字符串作为结果。

public function bark($up){
    return implode(array_fill(1, $up, $this->speak));
}

What you want to do is run the bark function in a for loop, not have a for loop in the bark function. 您想要做的是在for循环中运行bark函数,而在bark函数中没有for循环。

public function bark(){
  return $this->speak;
}

for($i = 0; $i < 2; $i++){
  echo $dog1 -> bark();
}

The problem with looping inside the bark function mostly comes from the fact that you lose a lot of flexibility. 树皮函数内部循环的问题主要是因为您失去了很多灵活性。

You could echo right in there, but outputting data from an object's method is far from ideal. 您可以在其中回显,但是从对象的方法输出数据并不理想。 What if you need to perform some kind of logic on the output? 如果您需要对输出执行某种逻辑该怎么办?

You could also concatenate within the function, but then again, what if you need to format the output? 您也可以在函数内串联,但是如果需要格式化输出,该怎么办? Then you'd have to split the output again, which defeats the purpose. 然后,您将不得不再次拆分输出,这将达到目的。

return inside the for loop exits from the bark function. bark函数的for循环出口中return So the dog barks only once. 所以狗只吠叫一次。

public function bark($up){
      $counter = 0;
      for($counter; $counter!=$up;$counter++){
         echo $this->speak;
      }
  }

$dog1 -> bark(2);

You have to change these lines: 您必须更改以下行:

return $this->speak;

To: 至:

echo $this->speak;

And

echo $dog1->bark(2);

To: 至:

dog1->bark(2);

The return statement stops immediatly the method, this is the reason you are getting only one echo return语句立即停止该方法,这就是您仅得到一个echo的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM