Jump to content

Do/While Loop


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi,

 

I'm learning PHP on CodeAcademy and for one of the lessons, I'm supposed to create my very own do/while loop. Here it is:

 

<?php
       $red = exciting;
         do {
              echo "<p>It's $red to be red.</p>";
       } while ($red == exciting);
    ?>

 

Not sure why it's not going through. It keeps giving me an error. Can you please help?

 

Best regards, ComputerColumbus :pirate:

Link to comment
https://forums.phpfreaks.com/topic/298664-dowhile-loop/
Share on other sites

You're getting an undefined constant error because php assumes exciting would be a constant. If you want to check if $red is equal to the sting of exciting you can either wrap it in single or double quotes.

<?php
       $red = exciting;
         do {
              echo "<p>It's $red to be red.</p>";
       } while ($red == "exciting");
    ?>
Link to comment
https://forums.phpfreaks.com/topic/298664-dowhile-loop/#findComment-1523582
Share on other sites

The loop you wrote is an infinite or endless loop because the loop will continue indefinitely as the conditional value never changes.

Add some way for $red to change so the condition will eventually equate to false and the loop will terminate.

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$red = "exciting";

$feelings = array(
'exciting',
'exciting',
'boring',
'exciting'
);

// use do-while loop when you want the code to
// run at least 1 time prior to checking the condition
do 
{
    // run statements
    echo "<p>It's $red to be red.</p>";
    
    // get value of $red
    $red = $feelings[rand(0,3)];
    
  // check condition  
} while ($red == 'exciting');
Link to comment
https://forums.phpfreaks.com/topic/298664-dowhile-loop/#findComment-1523584
Share on other sites

  • Solution

I tried adding at the bottom: $red = false; It's supposed to stop the loop, right? It still doesn't work.

Where did you add that line? It must be within the do {} statement not after the while condition.

$red = "exciting";
do {
    echo "<p>It's $red to be red.</p>";
    $red = false; // set $red to false to stop the loop
} while ($red == "exciting");
Link to comment
https://forums.phpfreaks.com/topic/298664-dowhile-loop/#findComment-1523602
Share on other sites

1 <!DOCTYPE html>
2 <html>
3  <head>
4        <title>Your own do-while</title>
5       <link type='text/css' rel='stylesheet' href='style.css'/>
6   </head>
7   <body>
8       //write your do-while loop below
9      <?p
10     $red = "exciting";
11      do {
12         echo "<p>It's $red to be red.</p>";
13         $red = false;
14    } while ($red = "exciting");
15   ?>
16   </body>0
17  </html>

 

This is the error message that it gives me: Parse error: syntax error, unexpected T_VARIABLE on line 10

Edited by ComputerColumbus
Link to comment
https://forums.phpfreaks.com/topic/298664-dowhile-loop/#findComment-1523608
Share on other sites

On line 9 you have an incomplete php tag, it should be <?php

 

Also on line 14 you need to use the comparison operator == not the assignment operator = 

    } while ($red == "exciting");
//                ^
//    needs to be the comparison operator

If used the assignment operator you will end up with an infinite loop.

Link to comment
https://forums.phpfreaks.com/topic/298664-dowhile-loop/#findComment-1523611
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.