4
<?php

$i = 0;

while(conditionals...) {

if($i == 0)
  print "<p>Show this once</p>";

print "<p>display everytime</p>";
$i++;
}
?>

Would this only show "Show this once" the first time and only that time, and show the "display everytime" as long as the while loop goes thru?

2
  • 2
    why not just run it yourself and see if it works? Commented May 6, 2009 at 20:16
  • I was asking this question for another person & sent them the URL to view the answer. Commented May 6, 2009 at 20:20

4 Answers 4

12

Yes, indeed.

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

if (!$i++) echo "Show once.";
Sign up to request clarification or add additional context in comments.

Comments

6

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>";
   }
?>

1 Comment

Adding a non-zero value inside the loop worked for me!! Thank you
2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.