Web Design / Database Project

Please note:

This material is adapted from Build Your Own Database Driven Website Using PHP and MySQL, by Kevin Yank. 2nd Edition, published by Sitepoint Pty. Ltd. ISBN 0-9579218-1-0. The book includes much more information, and you are encouraged to buy it!

Chapter 3 Notes, page 1

Introducing PHP

PHP is a server-side scripting language. In many ways, it is similar to JavaScript, as they both allow the embedding of scripts into your HTML pages that control what appears in the pages. The main difference is that PHP is interpreted by the Web Server before the page is even sent to the browser; JavaScript is interpreted by the browser. Also, once interpreted, the results of the script replace the PHP code in the web page, and all that the browser sees is an HTML file. There is a good example of a PHP script in Chapter 1 of the Yank text, so if you have not tried this, do it now.

There are advantages to using server-side scripting: no browser compatibility issues; access to server-side resources, which may not be available on the local computer (such as information stored in a MySQL database); and a reduced load on the client machine.

Basic Syntax and Commands

A PHP script consists of a series of commands, or statements, each of which is an instruction that the web server must follow before it goes on to the next one. PHP statements always end with a semicolon. For example:

echo ("This is a <b>test</b>!");

This statement calls a built-in function, "echo", and passes it a string of text. The echo function takes the text that it is given and places it into the HTML code at the current location. Here is an example; note that we have to open the file from the web server. We cannot just paste it into a browser location, because the web server has to process it. On this computer, I have the web server Microsoft Internet Information Server installed, so we can run this locally.

You have to use both the quotes and the parentheses. Quotes mark off the beginnings and ends of text strings in PHP. Parentheses indicate that "echo" is a function that you want to call, and they also mark off the beginning and end of a list of paramenters that tells the function what to do. Of course, some functions don't take any parameters, but you still need the parentheses.

Variables in PHP

Arrays in PHP

Arrays are variables that contain multiple values. PHP has a built-in function to create an array: "array." Yay!

Here is an array: $myarray = array('one', 2, 'three');
This array has 3 values: 'one', 2, and 'three' - two strings and a number. To access a value in an array, you need to know its index. Arrays use indices, usually numbers, to point to the values they contain. Indices start with zero, not 1. Once you know the index of the value you want, you can get that value by placing the index in square brackets following the array variable name: < br / >

You can change the value in an existing array element: $myarray[1] = 'Betazoid';. Also, you can add a new element to the array: $myarray[3] = 'Borg';. If you use empty square brackets, you can add elements to the end of the array: $myarray[ ] = 'Enterprise';.

Array indices don't have to be numbers. You can use strings as indices to create associative arrays. This type of array associates values with meaningful indices. For example:

So now, we have associated a date with each of the three names. If we want to know Miranda's birthday, we just look it up using the index:
echo ('My birthday is: ' . $birthdays['Miranda'];