PHP
From DreamHost
PHP, or PHP: Hypertext Preprocessor, is a general-purpose server-side scripting language. It can be embedded within, or used to manipulate, HTML pages.
Contents |
An Example of PHP
The following text, saved with a file extension of .php, will output "Hello, world!" if viewed with a web browser:
<?php 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:
<?php phpinfo(); ?>
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
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;
Loops
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
}
Example
Counts from 1 to 10 and displays result:
for($i=1; $i<=10; $i++) {
print $i."<br />\n";
}
Functions
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
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 four different PHP configurations:
- User-installed PHP running as a CGI application
-
PHP 4 running as an Apache module - PHP 4 running as a CGI application
- PHP 5 running as a CGI application
DreamHost no longer supports mod_php 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, and any domain which is changed away from using mod_php cannot be set back.
Switching between PHP4 and PHP5
See Fully Hosted settings in Domains Control Panel.
As of 8/26/08, it is no longer possible to switch back to PHP4, due to it being officially discontinued. Old domains that are working under PHP4 will continue to use PHP4 for as long as possible.
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
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 ~/.bash_profile.
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
- PHP Documentation - All the information you could ever need.
- PHP Patterns - Resource for more advanced PHP programmers.
- PHP Tutorial at Hudzilla - An excellent online and free tutorial on PHP.
See also
- Installing PHP5
- MySQL and PHP
- PHP Form Processing
- PHP Code Snippets
- PHP Classes
allow_url_fopen- Accessing files with external URLs.- PHP Magic Quotes
- PHP FastCGI
- Advanced PHP configuration
- Installing Tidy


