From SwinBrain

PHP is an interpreted server side scripting language used mainly for web development.

PHP is feely downloadable from


Hello World in PHP

    echo('Hello World');

Language Features

The following sections list of the PHP language features.

Basic Constructs


Variables are loosley typed in PHP. This means the following code is legal...

    $value = "A String";
    echo($value);//gives "A String"
    $value = 12;
    echo($value);//gives "12"

operators and functions


Some fun with arrays...

//create a simple array (default index = 0)...
$words = array("Humptey","Dumptey","sat","on","a","wall");
//basic for loop over array...
for($i = 0; $i < count($words); $i++)
	echo($words[$i] . "<br/>");
//foreach over array...
foreach($words as $word)
	echo($word . "<br/>");
//print individual array elements by index...
echo($words[0] . "&nbsp;" . $words[1] . "<br/>");
//redefine the array with arbitary index...
$words = array(10 => "Humptey","Dumptey","sat","on","a","wall");
echo($words[10] . "&nbsp;" . $words[11] . "<br/>");
//indexes don't have to be numeric...
$words = array("fname" => "Humptey");
echo($words["fname"] . "<br/>");

I wanted to be able to add a new element to an associative array after it had been created. Here's how...

$words = array("fname" => "Humptey");//create the array
$words["lname"] = "Dumptey";       //add a new element after creation
//and now to use them...
echo($words["fname"] . ' ' . $words["lname"] . '<br/>');

String handling

Simple string concatination uses the . (dot) to join strings, like ths...

    $value = "First String" . "Second String";
// should result in "First StringSecond String"
    $value = "First String" . "Second String";
    echo("The strings are " . $value);
// should result in "The strings are First StringSecond String" 

The paint wasn't even dry on this section before Pimaster was hassling me about the difference between sinlge quotes and double quotes. So here's some more demo code.

    $aWord = "blah";
    echo("the word is $aWord");//evaluates the variable
        //should result in "the word is blah"
    echo('the word is $aWord');//prints the variable name literaly.
        //should result in "the word is $aWord"

Single quotes can be good when you don't want things to be evaluated, or if you want to print special charecters like quotes. For example, if I wanted to use PHP to dynamicly insert an HTML tag like <nowiki><p align="center">variable text</p></nowiki> into a page, I could use...

        $someWords = "A long time ago, in a galaxy far away.";
	echo('<p align="center">$someWords</p>');//Prints "$someWords".
                                                 //Not what we want :(
	//echo("<p align="center">$someWords</p>");//No good either since the 
                                                   //quotes around the align 
                                                   //attribute confuse the parser :(
	echo("<p align=\"center\">$someWords</p>");//This works :)
                                                   //We have escaped the offending 
                                                   //quotes with backslashes.
	echo('<p align="center">' . $someWords . '</p>');//This works nicely.
                                                         //And it's easy to read :)

Don't forget we can mix markup and code. This next example uses a shorthand evaluation of variables that doesn't require the echo statement.

<?php $someWords = "A long time ago, in a galaxy far away."; ?>
<p align="center"><?=$someWords?></p>

PHP file Basics

We can include or require files, and this allows us to break our PHP code into modules or sections, and include them as needed.

Here's some examples of including files.

include 'extracode.php';
include ('extracode2.php'); // with brackets
include ("extracode3.php"); // you can use double quotes, but its a waste
include "extracode4.php"; // yet another variation 

If a file has already been included, then it will cause trouble if it tries to redeclare function's etc. Include_once() gets us out of this trouble.

include_once('myfile.php'); // 

There is also a require statement. This differs from include in that if the required file cannot be found or loaded, the script dies. If an included file cannot be loaded, the program will attempt to continue without it.

require 'filename.php';
require_once 'filename.php';

User Input

Any information sent between the client and the server is made available to PHP scripts by the PHP engine in special variables. Specifically, we have access to information sent to the server in special superglobal variables (listed below). The nice thing about superglobals is that they are automatically available in your functions and classes - they are not limited by the normal "scope" limits of other variables - hence the "super" and "globally" available name.

What information is available?

$_GET // query string information that is sent as part of the requested URL
$_POST // form information that is sent with method="post"
$_COOKIE // shared information that is part of the cookie tokens
// Now, because those three are a nice trio, and commonly used, 
// they are combined into a single superglobal variable:
$_REQUEST // contains $_GET, $_POST and $_COOKIE data
// There are also other superglobal variables for 
$_FILES // for up-loaded files from the client to the server
$_SERVER // for server information, such as paths and configuration info
$_SESSION // to store session information - if used. See sessions...
$_ENV // for environment variables, particularly relevant when using 
      // PHP as a console only scripting language... yes, you can do that.
Tip: Although there are other ways to access information sent from the client, such as the older (and dangerous) automatic global registration of variables, or the older $HTTP_GET_VARS etc, these are not recommended. Stick with the superglobals mentioned above for happier development. :)


Sessions and Cookies

// Start a session with 
// store or read session information in the superglobal variable
if(isset($_SESSION['user_name'])) {
  echo "<p>Hi {$_SESSION['user_name']}. Nice to see you!</p>";


File Handling

PHP and Databases


[edit]Language: C | Objective C | C++ | C# | Java | Pascal | Perl | PHP | Python | Visual Basic | more to come

Sample Code » C | C++ | .NET | Pascal | Perl | T-Sql | XHTML | Python | others to come