Help & Support: Customer Support
Help > Self Help > Webspace > PHP Guide

PHP Guide

To hold visitor interest and generate return visits, it's time to make your web pages truly interactive.

PHP is a freely available ('open source') scripting language that you can use with your CGI webspace and MySQL features, to create single webpages through to powerful, dynamically driven websites. Whilst cool graphics and animations can certainly add interest to an otherwise dull page, tailoring a page's content in response to a particular visitor offers a whole new outlook for the web developer. Together with your PlusNet CGI webspace and our beginner's guide, plus a few examples to get you started, why not inject some life to your website!

 

Overview
PHP, described by developers as a 'server-side, HTML-embedded scripting language', provides a powerful set of tools with which, web designers of all skill levels, can develop dynamic content for their web site. Created in 1994 by Rasmus Lerdorf as a personal project (PHP in fact stood for Personal Home Page Tools originally - although developers today refer to 'PHP Hypertext Preprocessor), it was enthusiastically embraced by a core team of developers and now is the most popular module for the industry standard Apache server, replacing Microsoft FrontPage and the Perl module as 'top-dog'. According to Netcraft (www.netcraft.com), as of September 2000, PHP had been installed on over 32% of Apache web servers worldwide (the systems that drive over 60% of the Web itself).

Why so popular? Well PHP is immensely powerful and developed specifically for web pages and their integration with databases. It is also 'open source' meaning that it is free to download and use. So rather than relying upon other popular methods of delivering dynamic content, such as Microsoft Active Server Pages and the licensing costs and NT systems that go hand-in-hand with it, people now have a low cost route to go down, which can only be a good thing.

And why use PHP rather than a CGI language such as Perl? Well that depends what you want to use it for. Whilst Perl is a very powerful scripting language that can be turned to an almost limitless number of applications, PHP's strength lies in how it can be embedded into your HTML code and how easily you can use it to query and display information taken from a database such as MySQL. PHP is also somewhat easier to debug, giving the line number where the error occurred rather than a cryptic '500 internal server error'.

It's quick as well. The PHP scripting engine has also been optimised for the response times that web applications demand, meaning much faster output to the browser than you might find with other systems.

 

Starting with PHP - the basics
If you are coming from a C, C++, Perl, Java or JavaScript background, you should find learning PHP fairly straightforward. It shares many similarities with Perl. For instance it uses typeless variables the way Perl does, prefixed with a "$" sign and holding any data type you wish. For example, $somevalue can be a variable that you can use to contain text strings or numbers. If $somevalue contained a number, you can increment its value using
$somevalue ++;
or
$somevalue +=1;
or
$somevalue = $somevalue + 1;


A major difference from Perl though lies in the fact that PHP was designed from the ground up to be used for scripting web pages. It has, as a result, many facilities built into it that you would have had to manually script if you were using Perl.

For example, do you want to send email to yourself from a form on the web page? In Perl, you probably would have to code something like the following:

open ( MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: myself\@mydomain.com\n";
print MAIL "From: visitor\@hisdomain.com\n";
print MAIL "Subject: Comments from Web Form\n\n";
print MAIL $mainmessage;
close ( MAIL );

In PHP, the same thing would be coded as follows:

mail ( "myself@mydomain.com", "Comments from Web Form",
$mainmessage, "From: visitor@hisdomain.com" );

Another convenience is in its handling of form input. If we take for example a form with a field like:

<input type=text name="dateofbirth">

You can immediately access that field with the $dateofbirth variable. There's no need to parse form inputs in this instance. All fields in the form are automatically converted to variables that you can access.

 

How to use PHP with your CGI webspace
Whether you know the basics of PHP already or are just getting started, you will need to understand how PHP is used with our CGI (Common Gateway Interface) server to run PHP content on your site.

The first rule to keep in mind is that to 'execute' or display all files containing PHP must be stored on the CGI server - cgi.username.plus.com. Otherwise, the PHP commands will be interpreted as standard HTML, producing errors when displayed on screen. This means that, whether you are running a PHP script or displaying a HTML file with PHP embedded, the PHP will not 'run' unless the file is stored in your dedicated CGI space.The reason for this is PHP is run as an interpreter program on our CGI server, with the specific intention of providing you with MySQL database interaction for your Web site.

When the PHP interpreter reads your file, it looks to see if there is any PHP code in the text. PHP commands should be surrounded by start <? and the end ?> brackets or tags. The interpreter will treat any code inside these tags as PHP script. Any non-PHP code that is enclosed by the tags should be 'echoed' so that it is just treated as 'text' rather than script. You can see this in Example 1 - Using PHP with simple MySQL database below, where the HTML tags are echoed.

There are two types of file that will use PHP code - a PHP script, and a HTML page with embedded PHP. These two different types use different formatting, so it's important to understand the differences between the two.

1) A PHP script
Written entirely in PHP, this is effectively an application which is executed on the CGI server, whether to display information on screen or to send data to a MySQL database. Any HTML displayed on a webpage has to be 'echoed', so it is displayed in the standard way onscreen.

2) A HTML file with PHP included
With PHP 'embedded' in a HTML page, it's both quick and easy to display dynamic content on your webpages. In Example 2 below, PHP has been included in a HTML webpage to control the colour of the page background and text. You will need to add opening and closing PHP tags (<? and ?>) when using PHP commands.

 

Summary of settings
  • Upload all files containing PHP to your CGI webspace - cgi.username.plus.com
  • Don't upload PHP scripts to the cgi-bin directory of your webspace. You should store (save) them on your CGI webspace outside the cgi-bin directory, or you can create a separate folder for PHP files.
  • Do upload PHP files in binary format.
  • Do CHMOD all PHP scripts to 755 or set to 'Execute', depending on the software you use (Available by clicking on the file with right mouse button in most FTP software). This isn't necessary for HTML pages containing PHP embedded.

To 'call' (run) your PHP scripts (with thanks again to Rosemary Powell)

If you are calling a script with NO user input, an ordinary link on your web page should work:

<A HREF="http://cgi.username.plus.com/your_script_name.php&qu= ot;>Run my script</A>

If you need to pass "fixed" values to the script, do:

<A HREF="http://cgi.username.plus.com/your_script_name.php?na= me=rosy&status=dozy">Run my script with parameters</A>

(Either of these first two ways you can also type the URL into the browser location box for testing)

Or, you can call it from a form (as per the usual CGI form options) if you want variable user input...

<FORM ACTION="http://cgi.username.plus.com/your_script_name.php&= quot; METHOD=POST>
<INPUT TYPE="radio" NAME="option1" VALUE="option 1" CHECKED> option 1
<INPUT TYPE="radio" NAME="option2" VALUE="option 2"> option 2
<INPUT TYPE="submit" NAME="button" VALUE="Run my script with user input!">
&nbsp;<INPUT TYPE="Reset" NAME="Reset">
</FORM>

 

Example 1 - Using PHP with simple MySQL database
To help you get started, the example below will show you how easy it is to get a PHP script online. Accessing databases is one of the main functions of PHP, a simple task that can be handled in 2 or 3 lines of code. There are built-in facilities in PHP to access MySQL, MSQL, Dbase, Oracle, InterBase, and so on.

If you are using the MySQL feature of PlusNet subscription accounts, then you'll most likely have received the following piece of code in your confirmation e-mail (the supplied code with thanks to Rosemary Powell).

<?php

$db_host = "host_name_here";
$username = "userid_here";
$password = "your_password_here";
$DB_name = "your_dbname_here";

echo "<HTML><BODY>";

$chan = mysql_connect ($db_host, $username, $password);
mysql_select_db ($DB_name, $chan);

$resultid = mysql_query ("select * from test", $chan);

while ($resultrow= mysql_fetch_row($resultid)) {
while (list($key,$value)=each($resultrow)) {
echo "$value ";
}
echo "<br>";
}

echo "</HTML></BODY>";
?>


This example script allows you to easily test query your database and write the results to HTML, displaying the results 'dynamically' to the web browser.
  • Copy the above script to a text editor such as notepad (or the excellent Textpad)
  • Edit the host name, user ID, password and database name to the details sent with your MySQL activation
  • Save it as 'mydbtest.php'.
  • Upload this PHP page from your computer into your CGI webspace at cgi.username.plus.com as a binary file
  • Set the file permissions (CHMOD) to 755, so that the file is executable. Most FTP software allows you to do this by clicking on the file with the right mouse button. If you've never done this before I would recommend using CuteFTP which makes both the uploading and CHMOD'ing of your files a doddle.
  • Call your page - your URL link is http://cgi.username.plus.com/mydbtest.php. You can link to this from one of your pages from your web space on ftp.plus.net of course, or just type it directly into a browser for testing.

Example 2 - PHP within HTML webpages
PHP can be treated as a CGI-like script as in the above example, or embedded into existing HTML. It is similar to JavaScript in this respect.

<HTML>
<HEAD>

<?
// A comment can be inserted like this
// Here we designate some variables
$bgcolor = "green";
$textcolor = "white";
?>

<TITLE>Another PHP example</TITLE>
</HEAD>
<BODY <? Print "bgcolor='$bgcolor' text='$textcolor'"; ?>>

<H3>Behold a PHP-enabled page :-)</H3>

</BODY>
</HTML>

  • Copy the above script into your text editor and save it as 'phpexample1.php'
  • Connect to your CGI webspace at cgi.username.plus.com to upload 'phpexample1.php' to your CGI webspace as a binary file
  • CHMOD to 755 (or Execute in FTP software such as CuteFTP).
  • Enter http://cgi.username.plus.com/phpexample1.php in your browser to see the result.

Worked? It's important to notice how the PHP and HTML code are differentiated, and even more importantly how PHP outputs code (such as 'bgcolor') that is ultimately interpreted to be HTML. However, this is only a very simple example of what PHP can do.

If you look at the source of the web page produced in the above example you'll get:

<HTML>
<HEAD>
<TITLE>Another PHP example</TITLE>
</HEAD>

<BODY bgcolor='green' text='white'>
<H3>Behold a PHP-enabled page :-)</H3>

</BODY>
</HTML>


As you can see, the actual PHP scripting commands are not visible on the client side. The actual creation of the HTML page is done on the server itself.

Example 3 - Dynamically create a colourful webpage
For the final page to try for yourself, here's a great example with explanation from Phil Davis at Webguys.com

<HTML>

<HEAD><TITLE>PHP Font Chart</TITLE></HEAD>

<BODY>

<TABLE>
<?
for($iR = 0; $iR <= 255; $iR += 51) {
for($iG = 0; $iG <= 255; $iG += 51) {
?>
<TR>
<? for($iB = 0; $iB <= 255; $iB += 51) { ?>
<TD>
<FONT COLOR="<? printf("%02X%02X%02X", $iR, $iG, $iB); ?>">
Font #<? printf("%02X%02X%02X", $iR, $iG, $iB); ?>
</FONT>
</TD>

<? } ?>
</TR>
<? } } ?>

</TABLE>

</BODY>
</HTML>

 

  • Copy the above script into your text editor and save it as 'webcolours.php'
  • Connect to your CGI webspace at cgi.username.plus.com to upload 'phpexample1.php' to your CGI webspace as a binary file
  • CHMOD to 755 (or Execute in FTP software such as CuteFTP).
  • Enter http://cgi.username.plus.com/webcolours.php in your browser to see the result.

Here's an explanation of how it displays so much with so little PHP code:
"The above simple program prints out Net safe colours, which are colours that display relatively similar in all browsers across all platforms. The program sets up an HTML page with three "for" loops, one for each of the RGB elements. The FOR loop is the most complex loop in PHP, it consists of three parts, or expressions:

FOR(expr1; expr2; expr3) statement

The first expression (expr1) is evaluated only once at the beginning of the loop. In the beginning of each loop, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statements are executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated. In our program we set expr1 in each of the loops to a variable ($iR, $iG, $iB) which holds a decimal number representing a piece of our Netsafe colours (0, 51, 102, 153, 204, or 255). In expr2 we compare our variable to see if it is greater than 255, if it is we exit the loop, if not we run the statements following the FOR loop and then evaluate expr3 which adds 51 to our variable using the "+=" operator. For example, in our first FOR loop we have "$iR += 51" this literally means take the current value of $iR, add 51 to it and then assign it back to $Ir. In addition to being more concise, it results in faster execution time.
The three loops together cycle through the 216 colours that will not dither in a web browser. Each time the second loop is run it prints a table row. Each time the third loop is run it prints a table element which includes a font tag with a PHP formatted print statement:

printf("%02X%02X%02X", $iR, $iG, $iB);

Printf allows us to output a formatted string consisting of our RGB variables. The first part of the printf statement is the format specification to use. The remaining comma delimited portions are variables, strings or numbers to print out using the format specification. Notice that the specification string repeats itself three times ("%02X"), once for each variable that we are outputting. The "%" marks the beginning of a format string. The next character is an optional padding specifier that says what character will be used for padding the results to the right string size. In our case we want all our hexadecimals padded with 0, hence the "0" in our format string. The next number is a width specifier that says how many characters (minimum) this conversion should result in, in our case "2". Finally we have a type specifier that says what type the argument data should be treated as. In this case we want to convert our decimal numbers to hexadecimal. We use the "X" in the format string to do this."

Tried that one? It's good isn't it?
There's so much that you can do with PHP it's difficult to know where to start. Here's some good links to get you going.

Useful links:

http://www.php.net/
http://www.zend.com/
http://php.resourceindex.com/
http://www.phpbuilder.com/
http://www.hotscripts.com/PHP/
http://www.webmonkey.com

Before you start making that all-singing all dancing dynamic web site though, bear in mind that your PlusNet CGI server is not designed specifically as a Web server.

A little forward planning will allow you to keep the bulk of your pages on the faster ftp.plus.net and use cgi.username.plus.com for the interactive and dynamic pages. You have a 50MB limit for your CGI space, which should be plenty for even the most ambitious project.



 Broadband
Email
Connecting
Features
Webspace
General

Can't find what you want? Check the in-depth help index

 Perform a search within the support or products section here.

Customer Support Statistics

 Getting connected
Stuck with setting up your Internet connection? Use our support guides!

Windows XP
Windows 2000
Windows 98
Windows 95
Windows NT
Linux
Apple
iMac
Other OSs

Sitemap

Products

Business Solutions

Member Centre

Community

Help & Support

About Plusnet

We sell broadband, phone, VoIP and more to homes and businesses in the UK. Winner of 9 out of 11 Categories in the 2008 USwitch survey. Winner of "Best Consumer ISP" at 2008 ISPA awards. Voted number 1 in the Broadband Choices 2008 survey.

© Plusnet plc All Rights Reserved. E&OE