PHPPHP stands for Hypertext Preprocessor. It is a general-purpose scripting language like perl but not as powerful. However, it is very popular among the website developer because it is especially designed for web development and can be embedded into HTML. Here we give some simple example of PHP program. First of all, dont forget to specify the interpreter the starting line: #!/usr/bin/php We begin with a simple "hello world" script
Save the script in hello.php and put it inside your public_html directory. Change the permissions to 700:
Then type www.doc.ic.ac.uk/~yourusername/hello.php in your browser to see how the script works. function echo simply prints out the information that follows it. $ _SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. ['HTTP_USER_AGENT'] is used to reference one of the element stored under this name. We can see that php code inside HTML, is surrounded by ' <?php' and '?>'. The code can be a single line function or in multiple lines:
phpinfo () is commonly used to check configuration settings and for available predefined variables on the server. Now we are ready for the guest book. This time we use a signle php that generates the form, if there is no imput from the browser, and create an entry in the book.txt if the 'name' field of the user input is longer than zero.
Click here to see it working PHP automatically stores variables from forms in the $_REQUEST array. If $_REQUEST['submit'] is set, that means that the form has been submitted, and the program goes into the first brunch and create an entry in the guestbook. If $_REQUEST['submit'] is not set, or the length of $_REQUEST['name'] is zero, the script generates the HTML for the form instead. Note that inside the <form> tag, the action of the script is set to the special variable $_SERVER[PHP_SELF], which is the path of the current page. This causes the form to submit to itself, a common and helpful technique that lets you put the logic to process a form in the same file with the HTML to display the form. The messages are saved into a file called book.htm, and then read in line by line using gets and send to brower using printf. The best things in using PHP are that it is extremely simple for a beginner. You can see yourself that the first brunch of the program where the user input gets handled is easier when compared to the perl version. However, PHP still offers many advanced features for a professional programmer. So don't get scared by the long list of PHP's features. By following this tutorial, you can start writing your first PHP program in just a few hours. Next Step: We look at some Java Server Pages. |