PHP is a popular web programming language available on nearly all hosting solutions. If you've got a decent grasp of C, then here is a handful of steps to jump start your knowledge of PHP.
PHP code enclosure
PHP is a text format that can be embedded in HTML. ALL PHP script blocks, whether they're stand-alone files or part of some HTML, begin with "<?php" and end with "?>". If you're familiar with ASP, this is akin to "<%" and "%>".
Step2
Understand C and/or C-style languages, PHP uses the same syntax: while(), for ( ; ; ), semi-colons to end statements, and curly braces to denote blocks and function bodies. C/C++ is especially helpful because many of PHP's built-in functions mimic those from the standard C library. JavaScript is also useful, because its loose typed variables and function declarations are similar.
Step3
PHP variables use dollar sign prefixes
Variables are preceded by dollar signs, even when used in function declarations. PHP uses the dollar sign to distinguish between variables and constants.
Step4
PHP strings can use apostrophe or quote characters
Strings can be enclosed in apostrophe ('single') quotes or normal ("double") quotes. Unlike JavaScript, this means either the value should be interpreted literally (single quotes) or with possible escape characters and/or embedded variables (double quotes). Concatenating/appending is done through a dot/period operator (.) rather than with the plus sign (+).
Step5
PHP echo() is main output function
echo() is your printf()/Response.Write() equivalent. Recall in the previous step the difference in quotes, if you run <?php echo('\r\n'); ?> you will see all four characters (slash, r, slash, n). If you run <?php echo("\r\n") ?> you will get the special CRLF character sequence. Also, you can embed variables (also known as expanding them) in double-quote strings: <?php $i = 7; echo("$i"); ?>. That will print the VALUE of $i, not the characters dollar and 'i'.
Step6
PHP $_GET[] is for query string parameters
Finally, the variables you'll be most interested in on the web will probably be $_SERVER[] and $_GET[] which are arrays of web server and query string variables. You use a string index to get the value, so you might write <?php echo($_GET['id']); ?> which would output the query string parameter "id".
Tips & Warnings
Practice with some simple loops and pages which manipulate input parameters before jumping into more complex things.
Use PHP.net to read up on functions you don't quite understand, there are use comments which can sometimes be really helpful.
Study the Array functions on PHP.net. I don't go into Arrays here because they're a subject all their own and you'll really want to know them.
Remember to use dollar signs in front of variable names, otherwise PHP will think they're constants.
Never directly output the value of an input parameter without first validating it. I've done it above for the purposes of compactness only.
Trying to concatenate two strings using the plus sign will result in a NUMERIC ADDITION.
Comments
Tricky said
on 6/27/2007 Very helpful. I just needed a quick-and-dirty to get me started.