deathlord55 Posted February 25, 2010 Posted February 25, 2010 Hello, I would like to keep this thread open to ask questions whenever i get stuck while i'm learning PHP and would really appreciate any help given, since i'm starting out low, my first few questions would probbably be really stupid. I started going through the W3schools PHP tutorials and got to the while loops section, Since it was pretty short i decided to make things a little bit more complicated for myself but in the end i didn't quiet manage to get it to work, would be nice if someone could point to the right direction of what i was doing wrong. What i was trying to do was basicly generate a random number and depending on the value of the number, either increment or decrement another variable which is used in the loop to create a luck based loop which could go on forever if lucky (or unlucky). I know it's not a good idea to do create long loops especcialy ones that totally depend on luck but i just wanted to get it to work and see what happens. This is the code i started with: $i = "2"; srand(time()); $k = (rand()%3); if ("$k =>2") { $i++; } else { $i--; } while ("1<=$i<=9") { echo "The number is ". $i . " "; } This didn't work so i tried to delete the if and try to work myself up so i did this: while ("1<=$i<=9") { echo "The number is ". $i . " "; $i++; } Which only worked with "" marks in but even after it "worked" it listed all the numbers it possibly can, crashing IE :(. I tried changing the condition to ("$i=>1 && $i<=9") But with the same crashing result as the above. I'm trying something impossible or i'm just doing stupid mistakes? Thanks Quote
a_bertrand Posted February 26, 2010 Posted February 26, 2010 That's certainly a wrong piece of code :D $i = 2; $k = rand(0,2); if ($k ==2) { $i++; } else { $i--; } while (1 <= $i && $i <= 9) { echo "The number is ". $i . " "; } - $i should be a number not a string here.... - srand(time) is not normally needed (http://php.net/manual/en/function.rand.php) - rand()%3 will return a number between 0 and 2 (inclusive) so your first if could be an == as it will never go above 2.... - Conditions cannot be "inside" a string (double or single quote) - There is no "in between" condition in PHP however you can merge multiple conditions with && (AND) and || (OR) So not much was correct... Sorry... Also, open a new thread for a new problem... Threads with 200 different posts are somewhat annoying. Quote
deathlord55 Posted February 26, 2010 Author Posted February 26, 2010 Thanks that worked, some things just can't be learned from a website :(. But thats why i try to do weird stuff when i'm learning codes because once i learn where my mistakes are, next time i know where to look for them. I see your point about lots of posts but i think it would actually be better than creating spam topics ^-^ I'm going to continue my learning tommorow. Quote
a_bertrand Posted February 27, 2010 Posted February 27, 2010 You are welcome. However I don't agree with you. I learned PHP just by reading the PHP manual (you may find it on php.net). Of course I must admit it wasn't the first programming language for me, so that could have made a difference. For the thread vs post, a different thread with a wisely choose title will be easier to find back for others. Also reviving a very old thread is not smart generally. Quote
Zero-Affect Posted February 27, 2010 Posted February 27, 2010 Thanks that worked, some things just can't be learned from a website :(. But thats why i try to do weird stuff when i'm learning codes because once i learn where my mistakes are, next time i know where to look for them. Trial and error is a good way ( i most of the time use trial and error but cross reference functions with php.net). I also disagree, i taught myself PHP from looking at code and visiting php.net 200+ times a day, there is always something new with php and that's mostly what attracts me to it. Good luck with teaching yourself PHP but maybe try some basic languages before hand (HTML). Quote
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.