ComputerColumbus Posted October 18, 2015 Share Posted October 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
valandor Posted October 18, 2015 Share Posted October 18, 2015 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"); ?> Quote Link to comment Share on other sites More sharing options...
hansford Posted October 18, 2015 Share Posted October 18, 2015 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'); Quote Link to comment Share on other sites More sharing options...
ComputerColumbus Posted October 18, 2015 Author Share Posted October 18, 2015 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. Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 18, 2015 Solution Share Posted October 18, 2015 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"); Quote Link to comment Share on other sites More sharing options...
ComputerColumbus Posted October 18, 2015 Author Share Posted October 18, 2015 (edited) 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 below9 <?p10 $red = "exciting";11 do {12 echo "<p>It's $red to be red.</p>";13 $red = false;14 } while ($red = "exciting");15 ?>16 </body>017 </html> This is the error message that it gives me: Parse error: syntax error, unexpected T_VARIABLE on line 10 Edited October 18, 2015 by ComputerColumbus Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 18, 2015 Share Posted October 18, 2015 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. Quote Link to comment Share on other sites More sharing options...
ComputerColumbus Posted October 18, 2015 Author Share Posted October 18, 2015 It worked, thank you everyone! Computer Columbus, Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.