Jump to content
Sign in to follow this  
ComputerColumbus

Do/While Loop

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:

Share this post


Link to post
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");
    ?>

Share this post


Link to post
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');

Share this post


Link to post
Share on other sites

Hell Valendor,

 

I added the double or single quotes. It still doesn't work. It has issue with line 1 ---> red = "exciting";

 

Hansford,

 

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

Share this post


Link to post
Share on other sites

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");

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.