PHP is a recursive acronym standing for PHP: Hypertext Processor. PHP executes on the server prior to the web server returning HTML code back to the client server. This guide will not go into HTML in detail, but because of its usage within an HTML environment, HTML will be used. Some specifics will be covered and some minimal explanation will be given.
Specifics of Processing
A typical scenario starts with the user executing a URL in their browser. The web browser connects to the web server and requests the file specified. If no file is specified, the web server looks for a file called index.html or index.php. PHP code can used within either type of file, but convention dictates to name the files with the php extension. When the web server reads the file it identifies the PHP code and passes the code to the interpreter. The results are then returned into the file. Once the file has been completely read into the server’s memory is send the file back to the user. Please note that the user never sees PHP code nor is there anyway for the user to know what the PHP actually is. All the user sees is the resultant HTML file. PHP code in a code block must be placed within the html tags of
<?php and ?>. Later we’ll see how PHP code can be mixed with HTML freely so the two can interact somewhat.
Hello World
The simplest type of program (born with Kernigham and Richie's seminal work The C Programming Language,introduced the world to the "Hello World" program. Basically, all this program does is return the words, "Hello World" to the user. To start, create a file called world.php. In this file type the following lines exactly as they are here:
<?php echo "Hello World"; ?>
Now, upload the file to the web server to the /scratch directory and go to the URL: http://www.schraderenterprises.com/scratch/hello.php.
There, that was simple (I hope!). echo is statement in PHP that returns anything in quotes to the user. All statements in PHP must end with a semi-colon. One frustrating aspect of PHP/Web development is the handling of errors. With compiled code, the compiler will give you a list of errors to give you some idea of where you have made a mistake. With PHP on the web, you often see a blank white screen or the White Screen of Death (WSOD). If you see this, go back and look at your code carefully for things like missing semi-colons or parenthesis. A common way of debugging scripts is to use comments. A comment allows you to right helpful information to the reader of a script without the interpreter (or compiler) executing that line. PHP supports two types of comments:
- A single line common line comment is created with
// - A multi-line common line comment is created with
/*and*/
. Anything between those two is commented.
Comments can be used to comment out suspect or buggy code or two add english descriptions to explain the code. Make it a practice to use comments frequently.
<?php // This is a Hello World Program echo "Hello World"; /* The lines below are commented out: foo bar hey-didly-do! */ ?>