Display text once within while loop on the first loop

后端 未结 4 521
半阙折子戏
半阙折子戏 2021-02-03 15:44
Show this once

\"; print \"

display everytime

\"; $i++; } ?>
相关标签:
4条回答
  • 2021-02-03 16:09

    Yes, as long as nothing in the loop sets $i back to 0

    0 讨论(0)
  • 2021-02-03 16:18

    Yes, indeed.

    You can also combine the if and the increment, so you won't forget to increment:

    if (!$i++) echo "Show once.";
    
    0 讨论(0)
  • 2021-02-03 16:29

    Rather than incrementing it every time the loop runs and wasting useless resource, what you can do is, if the value is 0 for the first time, then print the statement and make the value of the variable as non-zero. Just like a flag. Condition, you are not changing the value of the variable in between the loop somewhere. Something like this:

    <?php
    
       $i = 0;
    
       while(conditionals...) {
    
          if($i == 0){
            print "<p>Show this once</p>";
            $i=1;
          }
    
          print "<p>display everytime</p>";
       }
    ?>
    
    0 讨论(0)
  • 2021-02-03 16:32

    Yes it will, unless the conditions are false from the start or $i was set to 0 inside the loop

    0 讨论(0)
提交回复
热议问题