Does anyone know where I can find a list of PHP exercises? Kind of like homework/math problems. Where they get increasingly difficult and elaborate. I'm so much of a hands-on learner, that it's kind of hard for me to grasp PHP unless I have a boatload of exercises to just run through. Instead of reading the whole PHP.net manual. I would learn much better by have a problem laid out, then going and finding the information I need to solve that problem...know what I mean? Thanks!
Here you go. No idea how much you knew so I randomly made up some stuff. And no, they aren't elaborate at all, I'm just doing it for the fun of it haha Homework for awaken - Part 1 1. Define a variable called 'name' containing the text 'Mario' and then write "I am Mario." using the variable to write the 'Mario' part. You may only use two commands max! Hint: Variables in PHP are written as $variable 2. Use the code from the previous example but add a check using an 'if' condition to check whether the name is 'mario'. Place the text from the previous example in the 'if' condition. In other words: If the name is 'mario' write 'I am mario' 3. Try changing the $name variable to 'awaken'. You'll notice that the previous script writes nothing (If it doesn't, go back and redo #2, it's not complete!) In other words: If the name is 'mario', write 'I am mario' If the name isn't 'mario', write 'No, no, no. That is not my name!' -- Homework for awaken - Part 2 - Loops Remove the previous code (or save it) - The government might get suspicious of your awesome code skills! 4. Use a while-loop that starts at 0 and keeps running while the number is lower then 5. Write the variable to the screen after each run. Hint: First define a variable and increment it each time using $var++; (Overcourse: ++$var is faster) This is how it should look like on the screen when written correctly: 01234 5. Modify the previous example to use a 'for' loop instead of a 'while' loop Tip: You can scrap the variable you defined in the previous example, it is now used as a parameter in the for loop instead. Hint: For loops have 3 arguments. 1. Variable to define at start 2. Run while the following condition true. (Hint: Use the same as in the while loop) 3. What to do after a successful run. Quiz! 1. Which of the ways below are valid ways to start writing a comment? A. *** B. // C. /* D. # 2. Which of the following ways are not valid to output something to the screen? A. write B. print C. show D. echo Questions (These questions are good at times when you are not at a place where you can try your code. Try using google creativly to find solutions to the questions below) Some questions are overcourse, others very good to know. 1. There's two commands to start writing PHP code. <? and <?php - Which one is preferred, and why? 2. Here's a (kinda) common PHP error. What does it mean? Reading the error messages and thinking logic works most of the time! Parse error: syntax error, unexpected '}', expecting ',' or ';' in /home/atomicoz/test.php on line 5 Here's the code that produced the error for ($i=0; $i<5; ++$i) { echo $i } Code (markup): 3. OVERCOURSE (This is MySQL AND php. But if you want members or easier managing of your sites later on you will probably use mysql.) MySQL is a database for saving, getting, updating, removing information etc. For example to allow people to comment or add news, register as an user and log in.. and so on Your friend wrote this code and wants you to know whether it's good. It's used to login to a top secret government page. It queries the database (mysql) to check whether the username and password exists Example: file.php?username=USERNAME&password=PASSWORD ... $user = $_GET['username']; $passwd = $_GET['password']; mysql_query("SELECT * FROM users WHERE username='$user' and password='$passwd'"); ... Code (markup): You reply that this is bad code as there is no checking on neither the username or passwd variable. This is bad when sending stuff to mysql! For example people could use this link: file.php?username=admin&password=' or 1=1-- Code (markup): This would actually modify the ordinary SQL query and make it check if the password is either $passwd OR 1=1. 1=1 is always true, thus he gets in. How can he prevent this from happening?! Hint: .. Hmm, I heard somewhere that you can add slashes... I heard something about that you have to 'escape string' queries that are passed to the database End of test! If you completed everything, go to town and jump on a bench while singing "I'M A PROGRAMMER", preferably to some overly repetitive hard techno song to make the effect more dramatic. (I don't have the microphone plugged in so can't give you anything now, sorry ) -- Btw, no my name isn't Mario. I just came to think about that "It'sa me, mario!" phrase .. It's late, and I'm writing weird posts.. Time to sleep
http://www.hpcodewars.org/index.php?page=samples They're intended for Java not PHP, but many of them can be done in PHP (the logic side of them anyway) and are a great test for your coding brain
Hello there. I am having trouble with the first part of the task. Just wanna to know if this is right or not. <?php $mario="I am Marion"; echo $mario; ?>
I don't do PHP Lil_Viking but that seems right to me. The only problem I see is that you've added an n to Mario
Except that if you go to number two you will not get it right. If you save "I am Mario" as the name, then the second task will not become true, because if you compare Mario with "I am Mario", these are not the same. Do you follow me? In other words, what you had to save as the name, was the name it self. "I am" is not part of the name... after that, you need to compare the name with "Mario", so you can continue. Pretty kindergarden excercises, basically because there is not need to know PHP to understand a problem. There is need of logic though....
thx Awaken, you created a good thread but mosty thx woods you created an awesome post/reply about php beginner exercises, these execises doensn't have answers but its a good thing i found out that by just solving it and checking what php does it s better, i learn better than using answers. i don't know about you. Unlike math problems which you won't know the answer, php problems might give you an answer for example if the right text (i am mario) is displayed in your browser.
My solution for question 1 part 1: The best(cant say right because there is no right or wrong in programming as long as you get to a proper solution) way to do this imo should be the following: <?php $name ="Mario"; echo "I am $name."; ?>
My solution for question 2 part 1: <?php $name ="Mario"; if($name == "Mario") { echo "I am $name"; } else { echo "No, no, no. That is not my name!"; } ?> This one is true = I am Mario. <?php $name ="Luigi"; if($name == "Mario") { echo "I am $name"; } else { echo "No, no, no. That is not my name!"; } ?> This one is false = No, no, no. That is not my name!
My solution for question 3 part 1: Just change the $name ="Mario"; or the $name ="Luigi"; to $awaken ="Mario"; or $awaken ="Luigi";
My solution for question 1 and 2 part 2: <?php for($textsize=3,$number=0; $number < 5; ++$textsize, ++$number) { ?> <font face="Arial" size="<?=$textsize;?>"><?=$number;?></font><br> <? } ?> WARNING: I have added a easy little function that makes the text bigger for each time. On a webpage this code will look like this: 0 1 2 3 4
My solution for the QUIZ!: The correct ways are the following: //This is used for comments thats only 1 row. /*This is used for comments that needs more than 1 row.*/ #This can also be used for comments thats only 1 row.
My answer for Question 1: <?php is preferred since <? hasn't been used since PHP 4. My solution for Question 2: for ($i=0; $i<5; ++$i) { echo $i; //There should be a semicolon after the echo $i }
Anyone here still looking for PHP exercises? We've created a site just for such exercises at: http://phpexercises.com. It's got 26 exercises so far, including very beginning exercises with variables, moving on to control structures, forms, arrays and functions. Come help us out by trying them and letting us know if the instructions are clear! Thanks.