PHP

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: 

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: 

The Statement
if(expression) { // code to execute if expression evaluates to true }

Using the Clause with the   Statement
if(expression) { // code to execute if expression evaluates to true } else { // code to execute in all other cases }

Using the Clause with the   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 Statement
switch(expression) { case result1: // execute this if expression evaluates to result1 break; case result2: // execute this if expression evaluates to result2 break; default: // execute if no break statement has previously been encountered }

The Ternary Operator
(expression)?returned if expression true:returned if expression false;

The Statement
This loop will only be processed if the expression evaluates to true. while(expression) { // do stuff }

The ... 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 Statement
for(initialization expression; test expression; modification expression) { // code to be executed }

Example
Counts from 1 to 10 and displays result: for($i=1; $i<=10; $i++) { print $i." \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 }

Example
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 the following PHP configurations:
 * User-installed PHP running as a CGI application
 * PHP 5 running as a CGI or FastCGI application
 * 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.

You can change the PHP version on your domain in the Domains > Manage Domains section of the Dreamhost panel. There you'll click the Edit button next to your domain and then select the version next to PHP Mode. Each Domain and Sub-Domain can have a different version of PHP enabled.

Forcing PHP for HTML files
To process files with PHP whose names don't end with ".php", you can use a directive in .htaccess.

If you are using PHP 5.3.x as FastCGI you should use:

AddHandler fcgid-script .html FCGIWrapper "/dh/cgi-system/php53.cgi" .html

Instead, if you want to use PHP as CGI you should use:

AddHandler php5-cgi .html .htm

Note that if you use the second directive in a FastCGI environment you'll force PHP to run as PHP-CGI no matter what option you choose in control panel.

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 5 - /usr/local/php5/bin/php       (PHP 5.2.x)
 * PHP 5.3 - /usr/local/php53/bin/php    (PHP 5.3.x, where available)
 * PHP 5.4 - /usr/local/php54/bin/php    (PHP 5.4.x, where available)

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 PHP5.

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 ~/.bash_profile 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)

Advantages of PHP 5.x over earlier versions
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, 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.