PHP
PHP Basics
PHP is a powerful language that's easy to learn and use. It's embedded in your web pages - i.e. you put your code directly into the web page, delimited by special tags. You use one of the following tags to indicate to your webserver that what's contained is a block of PHP code, which needs processing:
<php> ..followed by.. </php>
<?php> ..followed by.. </?php>
<?> ..followed by.. </?>
The webserver software that's running on your webhost responds to requests from browsers by sending the requested web page. If that page contains PHP commands, then before the page is sent, the bit in the special tags is processed by the PHP processor, it's the output of your PHP code that's displayed - not the code itself!
PHP's syntax is similar to the 'C' programming language - the standard compiled language that comes with Unix. It's very widely used, in fact most of Unix itself is written in C. (C now has an object-oriented derivative called 'C++' that's also very popular.)
A example PHP statement is:
echo ("Hello World!\n");
Note that the statement consists of a function name ('echo' - prints text to the screen), followed by its parameters in round brackets. Strings are delimited by double quote marks '"'. The '\n' character means "New Line". The statement is terminated with a semi-colon ';'.
PHP has all the usual types, control structures, operators and functions that you'd expect to find in any general-purpose language. There are PHP functions to make most webmaster's jobs amazingly easy. PHP contains functions to access many different databases, it'll handle email and images, run files, manage cookies... It'll do just about everything you could need.
One particularly useful feature of PHP is that you can get a PHP file to run itself... This makes processing forms really convenient - as the form, and the code that checks it are all in the same file...
INFO:
PHP scripts ability to run themselves allow this sort of thing:
- The first time the form is called - all the variables (form fields) are blank...
The code checks this and runs some code to display the form.
- Your customer fills in the form, then presses submit.
- Submit is set to call the same script using this code (The $PHP_SELF variable is built-in to PHP!):
<form action="<? echo $PHP_SELF; ?>" method="post">
- The script now checks to see if the variables are set - they are...
It can now check all the fields in the form.
- If all the fields are OK it goes to the next page, or displays the next bit of code. (If it was a login page - it could display your user info for example).
- If there are mistakes, however, the display code is run again, but mistakes in fields can now be shown up in red (for example).
- Once submit is clicked again - the process repeats.
You'll see example of this method of form processing in many places on the 'net. You can achieve the same results with a Perl script, but it is easier in PHP. |
Web pages with SSI in them (see the chapter on Perl) usually need to be given a .shtml extension (i.e. index.shtml) otherwise they are not processed. Similarly PHP files need to have a .php, php3, .php4, .phtml extension before they'll be processed... If you have PHP code in a .html file - it'll get sent to the browser directly - it won't look pretty!
INFO: The reason why you have to give special file extensions to dynamic web pages is this:
- Normal .html files are just copied out by the webserver to the browser.
- PHP files must be processed by the webserver and the PHP engine.
- The PHP processing takes much more effort than the webserver's.
So, to avoid extra work - you tell the webserver which ones need the extra processing - by giving them a special extension. |
PHP Resources
The first PHP documentation set to get is from PHP.net. They provide a downloadable documentation set which is pretty good!
| Books (from Amazon.com) |
>Programming PHP by Rasmus Lerdorf, Kevin Tatroe |
Co-authored by its creator, Programming PHP is a nitty-gritty guide to PHP development. |
>PHP Pocket Reference by Rasmus Lerdorf |
This slim quick reference is a handy companion for all PHP developers. The author, Rasmus Lerdorf, is the creator of PHP, and PHP Pocket Reference is truly authoritative. It does not assume any prior knowledge and explains all the basics. |
>PHP Cookbook by David Sklar, Adam Trachtenberg |
The PHP Cookbook is a collection of problems, solutions, and practical examples for PHP programmers. The book contains a unique and extensive collection of best practices for everyday PHP programming dilemmas. |
>Professional PHP4 Web Design Solutions by Luis Argerich, Alison Gianotto, Raj Dash, Matt Anton, Jon Stephens, Bryan Waters, Jo Henrick |
This title shows web developers the versatility of PHP for large-scale web applications. It features case studies showing PHP use with output in HTML, XML, and WML. The PHP programming techniques are presented in such a manner that readers should be able to extrapolate the techniques (from both the theory and the case studies) for their own applications. |
>PHP and MYSQL for Dummies by Valade |
This is an introductory level book that provides the essential information needed to enable users to build common Web database applications.
The book is directed toward users who know HTML and have created static Web pages, but no prior experience with programming or database design or use is required |
>Web Database Applications with PHP and MySQL by Hugh E Williams, David Lane |
Web Database Applications shows Web developers how to build rich Web database applications using two leading open-source technologies, PHP and MySQL. The authors also assume use of the Apache Web server, which is by far the most common PHP scenario. Both PHP and MySQL are introduced from scratch, although this is a fast-paced book best suited to at least intermediate developers. |
|
|