1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP exercises

Discussion in 'PHP' started by awaken, Jan 8, 2008.

  1. #1
    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! :)
    SEMrush
     
    awaken, Jan 8, 2008 IP
    SEMrush
  2. woods

    woods Peon

    Messages:
    228
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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 :p)

    --
    Btw, no my name isn't Mario. :p I just came to think about that "It'sa me, mario!" phrase

    .. It's late, and I'm writing weird posts.. Time to sleep :p
     
    woods, Jan 8, 2008 IP
    Gatorade likes this.
  3. awaken

    awaken Guest

    Messages:
    149
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Woods, you are awesome!

    I really appreciate the help.

    Should keep me busy for a while. :)
     
    awaken, Jan 9, 2008 IP
  4. commandos

    commandos Notable Member

    Messages:
    3,652
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    JAY6390, Nov 8, 2008 IP
  6. Lil_Viking

    Lil_Viking Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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;
    ?>
     
    Lil_Viking, Jun 18, 2009 IP
  7. Xenonsoft Studios

    Xenonsoft Studios Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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 :p
     
    Xenonsoft Studios, Jun 29, 2009 IP
  8. Bohra

    Bohra Notable Member

    Messages:
    12,579
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    260
    #8
    its right most basic php
     
    Bohra, Jun 29, 2009 IP
  9. SunstarShop

    SunstarShop Peon

    Messages:
    582
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am sorry i am newer to php too, we can learn together!
     
    SunstarShop, Jun 29, 2009 IP
  10. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #10
    plog, Jun 30, 2009 IP
  11. Lil_Viking

    Lil_Viking Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks for the help.. :)
     
    Lil_Viking, Aug 7, 2009 IP
  12. CjKun

    CjKun Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12

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

    ;)
     
    CjKun, Feb 18, 2010 IP
  13. largn81

    largn81 Peon

    Messages:
    237
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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.
     
    largn81, Apr 15, 2010 IP
  14. PHPMyAdmin

    PHPMyAdmin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.";
    ?>
     
    Last edited: Oct 20, 2010
    PHPMyAdmin, Oct 20, 2010 IP
  15. PHPMyAdmin

    PHPMyAdmin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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!
     
    Last edited: Oct 20, 2010
    PHPMyAdmin, Oct 20, 2010 IP
  16. PHPMyAdmin

    PHPMyAdmin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    My solution for question 3 part 1:

    Just change the $name ="Mario"; or the $name ="Luigi";
    to
    $awaken ="Mario"; or $awaken ="Luigi";
     
    Last edited: Oct 20, 2010
    PHPMyAdmin, Oct 20, 2010 IP
  17. PHPMyAdmin

    PHPMyAdmin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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
     
    Last edited: Oct 20, 2010
    PHPMyAdmin, Oct 20, 2010 IP
  18. PHPMyAdmin

    PHPMyAdmin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    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.
     
    Last edited: Oct 20, 2010
    PHPMyAdmin, Oct 20, 2010 IP
  19. PHPMyAdmin

    PHPMyAdmin Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    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
    }
     
    Last edited: Oct 20, 2010
    PHPMyAdmin, Oct 20, 2010 IP
  20. jn2

    jn2 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    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.
     
    jn2, Oct 27, 2010 IP