PHP exercises

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

  1. tipuit

    tipuit Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #21
    i like to earn to do web site developed, and to do Photo shop design how can i get work?
     
    tipuit, Oct 28, 2010 IP
  2. kasun0777

    kasun0777 Well-Known Member

    Messages:
    355
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
  3. keyphp

    keyphp Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    I am going to develop a site for PHP, HTML, CSS, Jscript I hope it will help me and others in learning PHP etc, All of you can also contribute. it is keyphp
     
    keyphp, Dec 11, 2011 IP
  4. Jaxo

    Jaxo Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #24
    Unofficial answers, answered because I'm bored. Answers are in the smallest, most efficient way I could figure out.
    1)
    
    echo 'I am '.$name='Mario';
    //Bossing right through #1
    
    PHP:
    2)
    
    $name=strtolower('Mario');
    echo ($name=='mario')?'I am $name';
    //Ternary operators are cool looking AND efficient!
    
    PHP:
    3)
    
    $name=strtolower('Mario');
    echo ($name=='mario')?'I am $name':'No, no, no.  That is not my name!';
    //Expanding the ternary operator usage with the ternary 'else' statement
    
    PHP:
    4)
    
    $i=0;
    while($i<5){echo $i;$i++;}
    
    PHP:
    5)
    
    for($i=0;$i<5;$i++){echo $i;}
    
    PHP:
    Quiz:

    1. C and B. B is used for single-line comments, while C is used for block comments, spanning multiple lines.
    2. B and D. echo is better, as far as speed goes, but both are language constructs that don't really have much of a huge difference.

    Questions:
    1. <?php is preferred. <? is very often a very bad practice to use, since XML tags also open with <?. If you haven't aptly named your file with .php, or you have some weird server setup, your PHP could be parsed incorrectly; as XML, in fact.

    2. Well, there are two things wrong with this code, I'd say. One is personal preference, the other is the error.
    --The preference error is that you've used ++$i. Althogh this works, you're pre-incrementing the variable, which doesn't serve much different than $i++ (the standard) in for loops. Again, just personal preference.
    --The actual error is you've forgotten to put a semicolon after the echo $i in the third line.

    3. That is absolutely horrible code, in every way. Of course, it's meant to be. The many issues are as follows:
    --No input is sanitized. As a rule of thumb, treat ALL of your users like high-profile hackers. If it can be hacked, it WILL be hacked.
    --You're actually using a GET form for a login. Bad, bad, bad. Even if you do a lightning-fast redirect after the login, that visited URL is saved in your history.
    --You're using easy to sniff variable names like $username and $password. Network-sniffing hackers will be looking for these, so it's your best bet to change it to something at least a little different.
    --As for fixing these errors, run a mysql_real_escape_string on both inputs, change your form fields' names, use HTTPS (this IS a government login), and use POST instead of GET.

    End of test!
    
    $target=setTarget($_ENV['legs'],'town');
    walkTo($target);
    jump($_GET['bench']);
    $techno=analyze($_ENV['current']['song']);
    if($techno['type']==='hard'&&$techno['style']==='repetitive'){echo 'I\'M A PROGRAMMER';}
    
    PHP:
     
    Jaxo, Dec 14, 2011 IP
  5. Jaxo

    Jaxo Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #25
    On the contrary, I believe there IS a right way.

    The right way is the way that gives you the same result in the least amount of code. I could write this:
    
    $variable='this variable';
    if (strtolower($variable) == 'not this value') {
    echo 'Incorrect!';
    }
    else if (strtolower($variable) == 'not this one either') {
    echo 'Incorrect!';
    }
    else if (strtolower($variable) == 'still not getting the picture?') {
    echo 'Incorrect!';
    }
    else if ($variable == $variable) {
    echo 'Correct!';
    }
    else {
    echo 'Incorrect!';
    }
    
    PHP:
    Which is obscenely stupid, but it could be written as:

    
    $variable='this variable';
    switch(strtolower($variable)){
    case 'not this one either':
    case 'still not getting the picture?':
    default:
    echo 'Incorrect!';
    break;
    case $variable:
    echo 'Correct!';
    }
    
    PHP:
     
    Jaxo, Dec 14, 2011 IP