Results 1 to 33 of 33
  1. #1
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353

    PHP challenge #1

    Im going to start a series , there will be one challenge posted every day. Post your code (with comments) here. I will try and fix anyones errors and help them along.

    TODO #1.
    - Write a script to store and print your name (nick) and at least one favorite url to the browser using variables to assign them. Make the variable names conform to the rules explained in the PHP manual and use comments in the code explaining each line of code.

    Goodluck!
    Last edited by DoobyWho; 08-09-2002 at 03:12 AM.

  2. #2
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    PHP Code:
    <?
    $uName 
    'The Prohacker \n'//Define Username into uName for later use..
    $link 'http://www.webhostingtalk.com/'//Define the wanted link for later use..
    //This would be a return to keep the code clean and orderly...
    echo $uname $link//And volia, this is what prints out the username and link!!
    ?>
    Is that all your wanting? Or just starting out simple???
    -Mat Sumpter
    Director, Product Engagement
    Penton Media

  3. #3
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Good job prohacker. Im starting off simple so it can help people more. As the days/weeks progress, it will get harder and harder.

  4. #4
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    Originally posted by DoobyWho
    Good job prohacker. Im starting off simple so it can help people more. As the days/weeks progress, it will get harder and harder.
    Well I don't deserve a good job, considering I'm a professional programmer, I consider it cheating

    I was just wondering.. For comp 2 you might have people do arrays with the same info..

  5. #5
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Ok well you get a pat on the back for following directions ;-)

  6. #6
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Maybe I can get a sticky for these ;-)

  7. #7
    Join Date
    Jul 2001
    Location
    Australia
    Posts
    798
    Off-topic, but what's wrong with black writing.
    Andrew Walenczykiewicz
    StyleMy.com

  8. #8
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Nothing. lol. I just ... uhh.. want red.

  9. #9
    Join Date
    Jul 2002
    Location
    UK
    Posts
    169
    ummm......

    that wasn't a challenge even for a beginner. might aswell have asked them just to do hello world.

  10. #10
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    It'll get harder was we go. Bare with me. Im starting slow for the newbies.

  11. #11
    Join Date
    Jul 2002
    Location
    UK
    Posts
    524
    Will one of the later problems be "write me a signup form, kinda like phpmanager but customised for me", and then "write me a rival to cpanel, with my name and everything!"?

    Way to get free code

  12. #12
    Join Date
    Jan 2002
    Location
    Kuwait
    Posts
    679
    Originally posted by ubergeek22
    Will one of the later problems be "write me a signup form, kinda like phpmanager but customised for me", and then "write me a rival to cpanel, with my name and everything!"?

    Way to get free code
    Well, from the way I see it, this is why anybody would participate in the challenge. To give free code and improve himself and others.

    How about licensing the participants code in a "WHT Open Source License"?
    Ahmad Alhashemi
    PHP, Apache, C, Python, Perl, SQL
    18 related BrainBench certificates

  13. #13
    Join Date
    Apr 2001
    Location
    USA
    Posts
    270
    i could use a shopping cart....any takers?

  14. #14
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    lol. It's not like im going to use the code. Im already a master at PHP. lol

  15. #15
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Well since this is a learning forum, and I hope you don't mind this ProHacker, but I would not embed a space and a newline in a variable called $uName ...
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  16. #16
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Plus, DoobyWho, I'm surprised you have not spoken up about the error in The Prohacker's code.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  17. #17
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    Eck, I was up to late lastnight

    $uname = $uName eck, late night coding

  18. #18
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Hey that's ok, but I guess since DoobyWho is a 'master' he should be able to find the error...

    Mike, no master, prefers Python anyway
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  19. #19
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Oh, darn, you posted it. I was wondering how long it would take for others to pick up on it.

    Variables are case sensitive in PHP (and Python).
    http://www.php.net/manual/en/language.variables.php
    Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

    Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

    Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  20. #20
    Join Date
    Jul 2002
    Location
    New Hampshire
    Posts
    154
    First I'll say I'm not a great web developer so don't laugh too hard here. Why is putting a newline in a variable a bad idea?
    Sadisitkal
    When you say "I wrote a program that crashed Windows", people just stare at
    you blankly and say "Hey, I got those with the system, *for free*".
    -- Linus Torvalds

  21. #21
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    Originally posted by mwatkins
    Oh, darn, you posted it. I was wondering how long it would take for others to pick up on it.

    Variables are case sensitive in PHP (and Python).
    Sorry

    I'll leave out why the newline doesn't work

  22. #22
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    I think the thread is a good idea however, especially if hyperbole, bravado and ego are left at the door. Otherwise no 'beginners' will be attracted to the thread.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  23. #23
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    Originally posted by sadistikal
    First I'll say I'm not a great web developer so don't laugh too hard here. Why is putting a newline in a variable a bad idea?
    Sadisitkal

    Its not why I did it, but how I did that doesn't work

  24. #24
    Join Date
    Jul 2002
    Location
    New Hampshire
    Posts
    154
    Posted by mwatkins:
    Well since this is a learning forum, and I hope you don't mind this ProHacker, but I would not embed a space and a newline in a variable called $uName ...
    Thats the one I don't understand. Yes if your taking form input you probably don't want spaces etc but assuming its not form data I don't see the problem.

    Sadistikal
    When you say "I wrote a program that crashed Windows", people just stare at
    you blankly and say "Hey, I got those with the system, *for free*".
    -- Linus Torvalds

  25. #25
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Originally posted by sadistikal
    Why is putting a newline in a variable a bad idea?
    Sadisitkal
    No laughing allowed !

    For the example code - no reason why not. But in real life, you are bound to be using the variable $uName again, and again and again. Lets say you want to test the $uName against a table containing passwords:

    Code:
    table: users
    user_name   password
    mwatkins      de76ef0b283
    iamdead        a01g29feb
    
    and then a query...
    select user_name, password from users where user_name = $uName
    well... if $uName is equal to "mwatkins \n" that clearly is not going to retrieve any records.

    So, rule of thumb, $uName should contain a users name, not formatting characters etc., unless the variable truly is a throwaway item. But a variable called $uName doesn't sound or feel like a throwaway item, does it? No, in any larger script you are likely to be reusing it, and you don't want to be relying on your memory to remember how you have co-opted, used and abused various variables to do other purposes.

    Code:
    <?
    $uName = 'The Prohacker'; 
    $link = 'http://www.webhostingtalk.com/'; 
    echo $uName . '<br>' . $link; 
    ?>
    Not any more difficult and saves you having to remember to remove the ' \n' later.
    Last edited by mwatkins; 08-09-2002 at 02:29 PM.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  26. #26
    Join Date
    May 2001
    Location
    Dayton, Ohio
    Posts
    4,962
    Originally posted by mwatkins


    No laughing allowed !

    For the example code - no reason why not. But in real life, you are bound to be using the variable $uName again, and again and again. Lets say you want to test the $uName against a table containing passwords:

    Code:
    table: users
    user_name   password
    mwatkins      de76ef0b283
    iamdead        a01g29feb
    
    and then a query...
    select user_name, password from users where user_name = $uName
    well... if $uName is equal to "mwatkins \n" that clearly is not going to retrieve any records.

    So, rule of thumb, $uName should contain a users name, not formatting characters etc.
    But you have to think, he didn't mean for this code to be used as a login, and to just keep the code clean, you can do such a thing as include a new line...

    If this code was going to be used for other purposes yes, I might do something differnt...

    All personal prefernce

    Also if you look in my code, the echo'd varible was $uname, which won't work, because my varible is $uName, everything is case sensitive

  27. #27
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    It was like 5am , lol. Gimme a break ;-).

  28. #28
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Both pose very good points. However, when programming in any language you want to make sure your code will be easy to adapt to anything you might plan to do with it. (ie) you might not want to add in the newline character as it might interfere with later adaptions of the code. But as you said, all personal pref.

  29. #29
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    I know Prohacker - but isn't it interesting that people are asking the question - I think that's grand, and a good result of a thread like this. Stringing together bits of code gets simple with a little practice, but thinking like a developer takes longer for sure as I'm sure you know.

    I'm still getting there myself. My wife is the real pro developer in the house, I'm just a lowly systems integrator that generally writes lots of SQL and glue code.

    Although I do have a new content management system project nearing completion..
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  30. #30
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Now when this thread has advanced to the point when you are doing funky DB stuff or XMLRPC or SOAP, do let me know so we can have a PHP and Python bake-off ! ;-)

    Prohacker:
    Also if you look in my code, the echo'd varible was $uname, which won't work, because my varible is $uName, everything is case sensitive
    That reminds me of one thing I dislike about PHP - correct me if this has changed, but the code snippet with the incorrect use of case (uName vs uname):

    Code:
    $uName = 'The Prohacker \n'
    $link = 'http://www.webhostingtalk.com/'; 
    echo $uname . $link;
    
    Will output a new line and the link as follows:
    
    http://www.webhostingtalk.com/
    Note the blank line above the URI. What bugs me is that PHP does no validity testing to see if the variable $uname has ever been assigned to prior. Other languages would print out an error message.

    Is there any way to force PHP into some sort of "strict" mode to catch these errors? I found it very annoying on one project where I was cleaning up someone elses code (about 4000 lines of it) - there were many case related and unassigned variable related bugs in it.

    This issue, in PHP and any language, validity checking or not, certainly speaks to having a consistant naming scheme / coding style.
    Last edited by mwatkins; 08-09-2002 at 02:40 PM.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  31. #31
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Whats this conent management system called?

  32. #32
    Join Date
    Nov 2001
    Location
    Vancouver
    Posts
    2,416
    Currently its called pyCMS because I lack imagination. Its not a public project.

    I'm writing a Python / Postgres based solution for one of my web businesses (a subscription / fee based site) but doing it in a generic manner since I have a number of uses for this technology.

    Edit, ironically enough, the new code replaces a rather mutated and butt ugly PHP code base that I inherited. I liked PHP much more than ASP, but prefer the clean fresh taste of Python oh so much more.

    In my 'day job' I am a systems integrator working in the field of document, records, workflow, and content management.
    “Even those who arrange and design shrubberies are under
    considerable economic stress at this period in history.”

  33. #33
    Join Date
    Mar 2002
    Location
    Texas
    Posts
    353
    Ahh, i see

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •