From DreamHost
Revision as of 09:32, 12 January 2011 by Rlparker (Talk | contribs)

Jump to: navigation, search
The PHP Logo

PHP, or PHP: Hypertext Preprocessor, is a general-purpose server-side scripting language. It can be embedded within, or used to manipulate, HTML pages.

An Example of PHP

The following text, saved with a file extension of .php, will output "Hello, world!" if viewed with a web browser:

echo "Hello, world!";

Getting information about PHP

The language has a special function that can retrieve information about a particular installation of itself, and the environment in which it has been installed. This includes information about PHP compilation options and extensions, the PHP version, server information and environment (if compiled as a module), the PHP environment, OS version information, paths, master and local values of configuration options, HTTP headers, and the PHP License. Save the file with a .php extension, upload the file to a web server, and then view it with a conventional web browser:


Switching Flow

The if Statement

if(expression) {
    // code to execute if expression evaluates to true

Using the else Clause with the if Statement

if(expression) {
    // code to execute if expression evaluates to true
} else {
    // code to execute in all other cases

Using the elseif Clause with the if Statement

if(expression) {
    // code to execute if expression evaluates to true
} elseif (another expression) {
    // code to execute if the previous expression failed
    // and this expression validates to true
} else {
    // code to execute in all other cases

The switch Statement

switch(expression) {
    case result1:
        // execute this if expression evaluates to result1
    case result2:
        // execute this if expression evaluates to result2
        // execute if no break statement has previously been encountered

The Ternary (?) Operator

(expression)?returned if expression true:returned if expression false;


The while Statement

This loop will only be processed if the expression evaluates to true.

while(expression) {
    // do stuff

The do...while Statement

This loop will be processed at least once, and then continue if the expression evaluates to true.

do {
    // code to be executed
} while(expression);

The for Statement

for(initialization expression; test expression; modification expression) {
    // code to be executed


Counts from 1 to 10 and displays result:

for($i=1; $i<=10; $i++) {
    print $i."<br />\n";


Calling a Function

Note that this can apply to user-defined or built-in functions.

some_function($argument1, $argument2);

Defining a Function

function some_function($argument1, $argument2) {
    // function code

Returning Values from a User-Defined Function


In this example, the function is called with the arguments (parameters) "2" and "2". The result displayed is "4".

function add_numbers($num1, $num2) {
    $result = $num1 + $num2;
    return $result;
print add_numbers(2,2);

PHP on DreamHost

DreamHost currently allows four different PHP configurations:

  1. User-installed PHP running as a CGI application
  2. PHP 4 running as a CGI application
  3. PHP 5 running as a CGI or FastCGI application
  4. PHP 5 running as an Apache module (on DreamHost PS only)

DreamHost no longer supports mod_php on shared servers for these reasons (see the comment from Pete on the linked page for explanation). This functionality may still be available on some servers, but the ability to run mod_php may be removed at any time. New domains cannot be set to run mod_php on shared servers, and any domain which is changed away from using mod_php on a shared server cannot be set back.

Similarly, domains that are currently set to use PHP 4 will still run under PHP 4, but new domains cannot be set to use it, and setting a domain to use PHP 5 is an irreversible action.

Forcing PHP for HTML files

To process files with PHP whose names don't end with ".php", you can use a directive in .htaccess like this one:

  AddHandler php5-cgi .html .htm

PHP-CLI (Using PHP from the Command line)

DreamHost offers command line interface versions of PHP that is useful for developing shell applications with PHP. There are quite a few differences between the CLI (Command Line Interface SAPI (Server Application Programming Interface) and other SAPIs (such as PHP-CGI). It's worth mentioning that CLI and CGI are different SAPI's although they do share many of the same behaviors, and you can learn more about these differences by reviewing the PHP Manual's chapter titled Using PHP from the command line.

The paths for the default DreamHost PHP-CLI installed binaries are as follows:

  • PHP 4 - /usr/local/bin/php
  • PHP 5 - /usr/local/php5/bin/php (usually PHP 5.2.x)
  • PHP 5.3 - /usr/local/php53/bin/php (on servers running PHP 5.3)

You can confirm the exact version numbers in use by executing the binary from within the shell using the "-v" option, which will run the binary and return the version information. For example, you can check the version of the PHP5 CLI binary as shown below:

usernamer@server:~$ /usr/local/php5/bin/php -v
PHP 5.2.3 (cli) (built: Aug 21 2007 17:24:23) 
Copyright (c) 1997-2007 The PHP Group 
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
with Zend Extension Manager v1.2.0, Copyright (c) 2003-2006, by Zend Technologies
with Zend Optimizer v3.2.2, Copyright (c) 1998-2006, by Zend Technologies

The default PHP CLI interface on Dreamhost is PHP4. To use PHP5 append the full path of the php binary before your php script.

Make PHP5 the Default in the Shell

In some cases it may not be practical to have to specify the path to PHP5 for each CLI command. Instead, you may decide to make PHP5 the binary within the shell environment.

You can use an alias like so:

alias 'php=/usr/local/php5/bin/php'

Or you can edit your PATH environment variable like so:

export PATH=/usr/local/php5/bin/:$PATH

If you want these directives to be applied every time you log in, add either of the lines to your ~/.bashrc file

(Of course, you can modify this accordingly using the path to the PHP 5.3 PHP-CLI binary as well by using the path for the PHP 5.3 file as well)

PHP 4 versus PHP 5

PHP 5 offers several advantages over previous versions of the language. The most notable of these include the sophisticated support for object oriented programming (now comparable with Java), exception handling, better support for XML, and a simple, built-in database (SQLite). DreamHost has also taken the opportunity to alter default behavior of certain aspects of PHP (like the disabling of register_globals, which automatically creates global variables from submitted form data).

Output Buffer Cache

PHP functions flush(), ob_flush(), and ob_implicit_flush() will have no apparent effect on DreamHost. For performance reasons on a DreamHost shared host, output is buffered at a higher level than PHP (mod_gzip) and so these commands do not have any visible effect. If you need unbuffered output, you must contact Tech Support to request mod_gzip be disabled for your site.

External Links

See also