A VERY BASIC INTRODUCTION TO PHP FOR PEOPLE WHO ALREADY KNOW SOME PROGRAMMING CONCEPTS I'll start out by explaining some of the things I don't like about PHP. If I get this all out of the way, you can get to the useful stuff. I'll use this opportunity to say something instructive about how to use the language, based on its strengths and weaknesses, though, so it's not wasted time entirely. 1. PHP's syntax is weak. There will be times this will feel limiting. 2. PHP's primitive function set is huge. Really huge. 3. The combination of the previous two statements adds up to a situation wherein most PHP programming consists of writing a few often-used control statements, loops, and echo statements, using very basic I/O, and looking up functions to do what you want done. This means, of course, that it's fairly easy for unskilled programmers to do work -- and hard for skilled programmers to do good work. 4. Probably the language's most powerful inherent feature is its regular expression engine, which is incredibly weak compared to Perl's and has an obtuse, unwieldy syntax. 5. An external feature of the language that is more generally useful is the ubiquitous support for markup-embedded code on webservers. While other languages, frameworks, and development platforms offer similar functionality, PHP is basically everywhere. It tends to require jumping through fewer hoops to embed code in markup than most alternatives, too. Unfortunately, this means that a lot of the time web developers who would prefer to use another language are stuck with PHP. Luckily, the growing popularity of Ruby is changing that. 6. The core developers for the PHP project seem to have no idea what they're doing, in terms of a sane path of improvement. Huge, legacy code breaking changes are often introduced in "minor" version updates. I got bit by one of those with this very tutorial, breaking the tutorial's functionality -- and I didn't even know it happened for a long time. It's fixed now, thank goodness. 7. With "major" version updates the PHP team tends to doggedly cling to old ways of doing things that desperately need to be changed, supposedly for purposes of "backward compatibility". That might sound no worse than Java, if not for the obvious contradiction between this point and the last. 8. For some more, see the list of Problems with PHP [see note 1] over at ToyKeeper.net. 9. PHP is a ridiculously recursive backformation initialism that stands for "PHP Hypertext Preprocessor". OOP is an initialism that stands for "Object Oriented Programming". PHP + OOP = POOHP [see note 2]. For a first "assignment" to practice your incipient PHP skills after reading this tutorial, try writing a script that generates a webpage with this text file as the displayed content, formatted so that it is recognizable, readable, and clear on-screen. To test it, of course, you'll need to load the php file you write and the text of this page (in a text file) onto a webserver that supports PHP. You can download this textfile by right-clicking on the text file link at the top of this page and choosing the appropriate option from the context menu. The text file, of course, does not include either the navigation and ad banner area at the top, or the notes at the bottom, of this tutorial's webpage. They're not part of the "assignment". Note that some of what I say here may seem incomplete to an experienced PHP programmer. Keep in mind that this is meant to be a cursory beginner's tutorial to the language for people with programming experience in other languages. As such, it will both lack some more advanced information that is not suited to beginners and lack some more basic information that should be understood by programmers coming from any other language (with a couple of notable exceptions such as QBASIC and DOS batch file scripting). ##### embedding in XHTML: PHP is easily embedded in XHTML (or HTML) via some special script tags that identify their contents as PHP code. The best tag for this purpose, in terms of cross-platform compatibility, is generally , where "code" is whatever PHP code you want executed. The output of the embedded code is then inserted directly into the XHTML before it is sent to the client. ##### comments: Comments can be indicated by the methods common to C, C++, and shell scripting. /* This block of text is one contiguous comment. */ // Everything after those slashes on this line is a comment. # Same with everything after that hash mark. ##### variables and values: All variable names start with a dollar sign. There are no variable type declarations: their types are determined by what they contain. Variable names, after the dollar sign, can contain alphanumerics and underscores, and there are no arbitrary limits on the length of a variable name -- though I don't recommend using forty-character variable names for reasons that should be obvious. Variables are autovivified (created automatically, without necessary declaration) when you assign something to them, so no variable declarations of any kind are necessary, though initialization of variables can be used for clarity when writing your PHP code. $variable_name $_variableName $VARIABLENAME $variable123 Variables can be defined dynamically by stacking dollar signs. This is unlikely to be terribly useful to you, at least for a while, and shouldn't be used without good reason as it can make code harder to maintain, however. The following example code prints out "Hello, World!" in your browser (without the quotes). $variable = "Hello"; $$variable = ", World!"; echo $variable; echo $Hello; True and false values are represented as "true" or "false" when an expression is evaluated in a boolean context. In general, in PHP, a zero "0" character evaluates as false, as does a null value, while everything else evaluates as true, when using the normal equality test operator "==". There is a special equality operator "===" that evaluates "0" as true, however, if that's the behavior you prefer. Strings can be quoted with either double quotes, like "string", or single quotes, like 'string'. Using double quotes means that the PHP interpreter will try to interpolate escape characters and variables that are in the string. Using single quotes prevents that behavior. This means that single-quoted strings tend to get parsed faster (which means better performance for your code). When using double quotes, you don't have to escape any single quotes that are inside the string, and when using single quotes you don't have to escape any double quotes that are inside the string. This is something handy to keep in mind, when trying to operate on strings containing markup. In the following examples, the double-quoted version will have a variable in it and a newline escape character at the end as an example of how interpolated elements can be inserted into the code. Both examples will have both an escaped character of the same type as the quote marks and a non-escaped character of the other quote type. In PHP, escape characters are marked by a backslash. echo 'Visit Bob\'s URL!'; echo "Someone said \"Stop!\", but there were $count reasons to keep going, so I didn't.\n"; Array (list variable) elements are accessed using the element number inside [brackets], and start counting at zero. Thus, $file[0] is the first element of the $file array. You may specify the array element dynamically by using a variable to hold array element numbers, such as in $file[$element]. Arrays can also be indexed by strings rather than numbers, in what PHP calls "associative arrays", but Perl calls "hashes", such as with $file["first_line"]. New elements can be tacked onto the end of a non-associative array without having to specify the element's index number by simply omitting the number, as in $file[] = "value". String variables can be accessed as though they were arrays as well, as a side-effect of the fact that strings and arrays are undifferentiated in both appearance and type safety. In this example, both lines of code create an array element $foo[2] that contains the letter "u". $foo = "Four score and forty fours ago."; $foo[2] = "u"; Variables can be passed via the URL in your browser's address bar. You should be careful about how you do this, as it can potentially pose an unnecessary security risk. http://www.domain.com/index.php?foo=37 http://www.domain.com/?foo=37 This can make it easy to pass variables from one page to another. link text In recent versions of PHP, you may have to explicitly capture the values of those variables from the URL: PHP uses dynamic scope. This means (among other things) variable scope is within the current block only, by default, unless the variable is accessed with the global keyword. Without using the global keyword, global variables will not be accessible from within your function because it assumes you want to use a local variable. Variables that are not within a block or function have global scope, as do variables passed via the URL. It's usually best to pass values from global variables as arguments into a function, and return values from the function, rather than use the global keyword to access the global variables directly, however. You may note in the following example that there is both a function and a variable called "foo". The two are unrelated, and do not conflict because the dollar-sign syntax differentiates the variable from the function. Function names can, however, conflict with the primitive (built-in) functions and keywords of the language. Don't name a function "global". $bar = 2; function foo { $foo = 1; global $bar; return $foo + $bar; } ##### output: A common means of producing output from a PHP script is echo(). It pretty much works as expected. $string = "Four score and seven years ago."; echo $string; echo "Four score and seven years ago."; ##### including and requiring other file contents: The include() and require() functions are used to access the contents of other files -- often, other PHP files that contain code one needs to execute within the current file. They can thus be used for rudimentary code library handling, or alternatively to simply and easily grab text from content files. The difference is that include() will throw an error/warning if the file to include does not exist, while require() will cause the script to fail and cease executing if the file cannot be found and read. There are also include_once() and require_once() functions which behave in exactly the same manner, except that they check to see if that file has already been included or required, and if so, they don't do so again. Once read into the file, anything inserted by an include() or require() is then parsed as though it were written into the file in the first place. $file = 'file.txt'; include("file.txt"); include $file; require($file); require "file.txt"; include_once("file.php"); include_once "file.php"; require_once("file.php"); require_once "file.php"; Another means of getting file contents into your script is to assign them as an array, delimited by newlines in the file, by use of the file() function. Thus, text is read from the file by the file() function, whose return value is a list that can be assigned to an array variable. $contents = file("file.txt"); $contents = file "file.txt"; ##### basic operators: + addition - subtraction * multiplication / division = assignment == equality comparison != inequality comparison ++ incrementor += increment by -- decrementor -= decrement by . concatenation .= concatenate by < less-than comparison > greater-than comparison <= less-than-or-equality comparison >= greater-than-or-equality comparison && and || or This is not a complete list, but it's enough to get you started. ##### non-looping control structures: if/elseif/else if($foo == $bar) { do stuff; } elseif($foo > $bar) { do other stuff; } elseif($foo < $bar) { do another thing; } else { do your laundry; } The if/elseif/else control structure allows for a completely different syntax. Note the lack of braces and the use of colons. if($foo == $bar): do stuff; elseif($foo > $bar): do other stuff; elseif($foo < $bar): do another thing; else: do your laundry; endif; switch statement switch($foo) { case "string": do stuff; break; case $bar: do stuff; break; default: do stuff; break; } Similarly to the if/elseif/else, braces can be avoided with an alternate syntax. switch($foo) case "string": do stuff; break; case $bar: do stuff; break; default: do stuff; break; endswitch; ##### loops: Highly useful for dealing with arrays, the foreach loop has a simple structure. An array is related to a scalar variable name on which the contents of the loop operate. Both "normal" linear arrays and associative arrays can be accessed in this manner for iteration through their contents. foreach($foo as $bar) { echo $bar; } There's an alternate syntax, of course. foreach($foo as $bar): echo $bar; endforeach; The while loop is simple and handy. while($foo == $bar) { do stuff, hopefully including a change in the value of $foo or $bar eventually; } An alternate syntax exists for this as well: while($foo == $bar): do stuff, et cetera; endwhile; There are also do/while and for loops. ##### functions: Defining a function is easy enough. function foo($bar, $baz, $qux) { echo "$bar and $qux are being printed to the screen."; return $bar + $baz; } Invoking and passing arguments to your function is easier. $capture = foo(7, $foo, "time"); ##### regular expressions: You can use regular expressions for patterns-based text substitution. For instance, the preg_replace() function uses a (nominally) Perl-like syntax for a substitution regular expression (regex for short -- plural, regexen). The preg_replace() function takes three arguments, including (in order) a pattern to match, a pattern with which to replace matching text, and a string on which to operate. The matching regular expression can be specified between slashes, and use backslash-escaped special characters, such as \s for any single whitespace character, \S for any single non-whitespace character, and so on. A period represents a single character of any description at all. Regular expressions can (and do, on occasion) fill entire books with explanations of their use, so I'll leave this pretty cursory for now. The "preg" in preg_replace() stands for "Perl-style REGular expression", by the way -- and there are non-Perl-style regular expression functions in PHP as well. $string = 'Four score and seven years ago.'; $string = preg_replace('/yea/', 'bee', $string); Other delimiters than forward slashes can be used when defining regexen. The vertical bar, or pipe -- the | character -- is often useful when matching markup such as XML, XHTML, and HTML, so that your delimiters don't conflict with closing tags. More information about regexen can be found elsewhere on the Web, and entire books have been written on the subject (as I mentioned above). ##### some PHP primitive functions: nl2br() turns newlines in a string into
tags $formatted = nl2br(include("textfile.txt")); shift() removes, and returns, the first element of an array $first_element = shift($array); join() specifies a join-string to combine elements of a list into a single string $string = join('', $array); // creates a single string from array // elements without any spaces or other // characters between the joined parts htmlentities() translates all applicable characters in a string into HTML entities so that they will be displayed appropriately rather than being parsed by the browser; there are a number of different functions that behave similarly, and this is only one example $formatted = nl2br(htmlentities(join('', file("textfile.txt")))); show_source() outputs (X)HTML-formatted context-highlighted display of source code for the file indicated in the function's arguments show_source('filename.php');