# PHP

Resources:
* book chapter
* PHP 5 Tutorial
* PHP 5 Reference
* PHP Project Homepage
PHP (stands for PHP Hypertext Preprocessor) is a programming language for processing web requests. When you use a browser to request or fetch a web page you are sending a request to the server. The web server know how to use the URL to find the file and send it back to your browser. PHP allows us to create programs on the web server and when the programs are requested the server send the output from the programs as the response. For this to work, the web server must know how to handle PHP files. Most web servers have the PHP module installed and when they see a request for a PHP file they will forward the request to the PHP module. The PHP module will parse the request parameters and invoke the requested PHP code.
This is a powerful idea. With PHP you can create programs on a web server that can dynamically create web pages using data sources from files to databases to other web services. Even though it is a simple request-response model on top of the web client-server model, we can be create powerful web applications by breaking down complicated interactions to a series of request-response cycles.
PHP is a scripting language. We create our programs as text files, which are also the executables. When a program is executed the PHP interpreter reads the source/executable file and execute one line of code at a time. Your PHP code can be tested on the server by using the interpreter as follows:

## PHP Basic Syntax

In a PHP file you can intermix PHP code and static content. When the file is executed only the PHP code gets executed. The output from the code replaces the code part forming the output of the program with the static content intact. This design makes it easy to generate web pages using a template and injecting dynamic content into parts of the page.
A PHP code block is defined using a tag as shown in the example:
Please check out the following tutorials on w3schools.com to learn basic PHP syntax:
* code block and comment
* variables
* echo and print
* data types
* operators
* if-else
* switch
* while/do-while loop
* for loop

### Examples

The HTML generate by the previous PHP script is as follows:
The indentation is not what we expected and we could print the space or the tab characters from the PHP script but it would be very tedious. Instead we can use expression blocks to inject dynamic contents to the static ones as shown in the following example.

## PHP String Functions

Functions are reusable blocks of code. PHP has a set of build-in string functions for manipulating strings. A partial list of string functions is as follows:
* strlen
* strpos
* substr
* strcmp
* explode
* implode
* complete list of string functions.
The explode function allows us to "unpack" a string into a array of substrings. The following code
will output
You can name the substrings using the list notation. The following code
will output
The implode function packs a array of strings into a single string.

## PHP Array Functions

Python array allows us to refer to a collection of items using a single variable. The items can be of any datatype including array (how multi-dimensional arrays are created). The size of a Python array is not fixed. In fact there are functions that allow you to treat an array as a queue or a list. Some example array functions are as follows:
* array
* count
* list
* array_push
* array_pop
* array_slice
* array_splice
* array_shift
* array_unshift
* sort
* rsort
* complete list of array functions

## PHP Global Variables

PHP superglobal variables are a set of predefined variables that are always accessible to all PHP scripts on the server.
They are:
* $GLOBALS
* $_SERVER
* $_REQUEST
* $_POST
* $_GET
* $_FILES
* $_ENV
* $_COOKIE
* $_SESSION
The following script
will output content similar to
As you can see the server gets all kinds of information from the client through the HTTP request sent and makes the information available to the PHP scripts via the $_SERVER variable.
If we request the following script using the following URL: http://host/path/to/test.php?first_name=Curious&last_name=George the script will echo back the parameters sent via this request.
The actual output is
$_REQUEST is an associative array with strings as indices. You can refer to the elements in the array by their names, for example $_REQUEST["first_name"] will give you the value of "Curious". If you expect a parameter to be sent to your script via the query string you can attempt to get it through this $_REQUEST variable. The following script test whether the parameter is sent and echoes "first_name parameter is missing." if the parameter is not provided in the query string.

## PHP File Functions

PHP file functions allows us to access and manipulate the filesystem on the server on which our PHP scripts are executed. The basic file functions include reading content from a file or a directory and writing to a file. Some commonly used file functions are as follows:
* file
* file_get_contents
* file_put_contents
* glob
* basename
* complete list of file functions
The following PHP script reads a poem from a file and outputs it as a ordered list.
The output will appear as follows in a browser window:
Reading a directory is similar to reading a file except that the result is a list of files and directories of the directory we are reading. The glob function allows us to scan a directory to read items (file or directory) that matches a pattern. The following script reads names of text files that are in the "word" subdirectory, which is under the directory that contains the script.
As shown in the output below the array returned from the glob function contains relative paths to the files, which makes it easy to read the files (next example).
The next script takes the paths to the files, reads the file contents, and echoes them back to the caller.
A sample output of the script is as follows. The basename function returns the path's last component, which is the "bare" file name.

## PHP Include Statement

PHP include statement copies the content of another file into the current file where the include statement appears. For example, the following statement will "include" the output of the navigation.php script (e.g. HTML code for a navigation bar) into its own output.