Web Hosting Talk







View Full Version : PHP challenge #1


DoobyWho
08-09-2002, 03:03 AM
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!

The Prohacker
08-09-2002, 03:10 AM
<?
$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???

DoobyWho
08-09-2002, 03:14 AM
Good job prohacker. Im starting off simple so it can help people more. As the days/weeks progress, it will get harder and harder.

The Prohacker
08-09-2002, 03:17 AM
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 :D

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

DoobyWho
08-09-2002, 03:19 AM
Ok well you get a pat on the back for following directions ;-)

DoobyWho
08-09-2002, 03:45 AM
Maybe I can get a sticky for these ;-)

NetXL
08-09-2002, 04:07 AM
Off-topic, but what's wrong with black writing. :)

DoobyWho
08-09-2002, 04:09 AM
Nothing. lol. I just ... uhh.. want red. :D

dynamitehost
08-09-2002, 06:34 AM
ummm......

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

DoobyWho
08-09-2002, 06:35 AM
It'll get harder was we go. Bare with me. Im starting slow for the newbies.

ubergeek22
08-09-2002, 07:41 AM
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!"? :D

Way to get free code ;)

Ahmad
08-09-2002, 09:10 AM
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!"? :D

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"? :stickout

jhcashman
08-09-2002, 10:47 AM
i could use a shopping cart....any takers? :stickout

DoobyWho
08-09-2002, 11:00 AM
lol. It's not like im going to use the code. Im already a master at PHP. lol

mwatkins
08-09-2002, 12:51 PM
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 ...

mwatkins
08-09-2002, 02:01 PM
Plus, DoobyWho, I'm surprised you have not spoken up about the error in The Prohacker's code.

The Prohacker
08-09-2002, 02:07 PM
Eck, I was up to late lastnight :D

$uname = $uName eck, late night coding :D

mwatkins
08-09-2002, 02:09 PM
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

mwatkins
08-09-2002, 02:13 PM
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).

sadistikal
08-09-2002, 02:14 PM
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

The Prohacker
08-09-2002, 02:15 PM
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 :D

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

mwatkins
08-09-2002, 02:16 PM
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.

The Prohacker
08-09-2002, 02:16 PM
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 :D

sadistikal
08-09-2002, 02:20 PM
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

mwatkins
08-09-2002, 02:21 PM
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:


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.


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

The Prohacker
08-09-2002, 02:24 PM
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:


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 :D

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 :D

DoobyWho
08-09-2002, 02:24 PM
It was like 5am , lol. Gimme a break ;-).

DoobyWho
08-09-2002, 02:27 PM
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.

mwatkins
08-09-2002, 02:29 PM
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.. ;)

mwatkins
08-09-2002, 02:31 PM
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):

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

DoobyWho
08-09-2002, 02:33 PM
Whats this conent management system called?

mwatkins
08-09-2002, 02:43 PM
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.

DoobyWho
08-09-2002, 02:57 PM
Ahh, i see :)