Approaches to Web Development for Bioinformatics

Previous  Contents  Next
References

Language Basics

Here is the basic hello world program in PHP from the PHP manual 35.


PHP

<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>

The first five lines in the script are HTML tags.  The PHP bit starts on the sixth line.  The syntax <?php tells the PHP processor to start processing a script.  The script in this case is the line


echo "Hi, I'm a PHP script!";

This script is executed by the PHP processor on the server.  The output of the script is the HTML page with the "Hi, I'm a PHP script!" inserted in the place of the <?php... ?>. . The script looks like this screenshot in a browser.

Screenshot of PHP Hello World Script

To use PHP your web server has activated support for PHP and that all files ending in .php are handled by PHP. On most servers, this is the default extension for PHP files.  Popular Linux distributions, such as Red Hat, and popular ISP hosting services will have this configured by default.  For the example above, save the file as hello.php and put it in any public directory on your web server.

Here is a useful program to print out the server environment to a HTML page:


PHP

<html>
<head>
<title>PHP Info</title>
</head>
<body>
<?php phpinfo(); ?>
</body>
</html>

This will print out a long list of server information beginning with this screenshot:

Screenshot of PHP Server Information

Getting started with HTML forms is very easy with PHP Here is simple example that shows how to process a basic form. The form page is straight HTML.


HTML

<html>
<head>
<title>Simple Form</title>
</head>
<body>
<form action="simple_form_processor.php" method="post">
<p>User ID: <input type="text" name="name" /></p>
<p><input type="submit" value="Submit"/></p>
</form>
</body>
</html>

This prompts the user to input his or her name and posts to a PHP page called simple_form_processor.php. A screen shot is shown below.

Screenshot of a Simple Form

The PHP script that processes the post request checks to see if the user's name is 'Alex'.


PHP

<html>
<head>
<title>Simple Form Processor</title>
</head>
<body>
<?php
if ($_POST['name'] == 'Alex')
echo '<p>Hello Alex</p>';
else {
echo '<p>You are not authorized to proceed</p>';
}
?>
<p><a href='simple_form.html>'>Go back</a>
</body>
</html>

The result is shown in the screen shot below.

Screenshot of a Simple Form

The value of the user ID is retrieved with the $_POST['name'] variable an used in an if statement.  If statements in PHP have the same syntax as Java and C.  The use of a semicolon is also similar to Java and C.  However, for the last statement in a block the semicolon is optional.


Previous  Contents  Next
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies