PHP Code SyntaxAs we said above, we'll start with an overview of PHP syntax. So far in this book, you'll have seen many examples of PHP code, as added by Dreamweaver MX into your pages. However, you haven't really had to write any yourself, or understand exactly what the code does. While you can do a lot using Dreamweaver MX in this way, it is also useful to have a basic knowledge of PHP syntax in order to edit the PHP code that Dreamweaver MX adds or to write your own.
PHP is similar to other server-side technologies, in that the PHP code has to be placed between special tags, to
Anything between the above PHP tags will be interpreted as PHP code by the PHP compiler. You can use any of the above styles of tag syntax, although we will be
StatementsNow to the PHP code itself. Much of the code consists of single statements. These are written on a single line, and are followed by a semicolon, ; , as shown in the example below:
<?php echo "This is a test line showing PHP syntax" ; ?> This statement prints the text This is a test line showing PHP syntax to the page.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Variable |
Value |
|---|---|
|
$example1 |
The total is $total |
|
$example2 |
The total is 10 |
As you can see, text
For $example2 , where the text is enclosed in double quotes, the value of $total is substituted into the text.
PHP has a handy way of appending one string to another using the concatenation operator "." , for example:
<php $a = 'apples'; $b = 'bananas'; $c = ' ' ; $c = $a . ' and ' . $b; ?>
In this example, $c would have the value "apples and bananas" .
You can also quickly append one string to another using ".=" , for example:
<php $a = 'apples'; $a . = ' and bananas'; ?>
$a would now have the value "apples and bananas" .
Escaping a character
This is an example showing double quotes (" ")
If we try and insert this into a variable, and print it to the screen, as in the following example, we will get an error:
<?php $example = "This is an example showing double quotes (" ")"; echo "$example"; ?>
This causes an error, because the PHP parser gets
<?php
$example = "This is an example showing double quotes (\" \")";
echo "$example"; ?>
As we have preceded the quotes in the string with the backslash, they are ignored by the PHP engine and printed to the screen, as we intended. This method should also be used if you want to include some quotes in a string that is to be inserted into a database.
Below is a list of the characters that need to be escaped in this way.
|
Character |
Escaped Character |
Description |
|---|---|---|
|
not
|
\n |
Adds a
|
|
not applicable |
\r |
Adds a
|
|
not applicable |
\t |
Adds a tab |
|
\ |
\\ |
Backslash |
|
$ |
\$ |
Dollar Sign |
|
" |
\" |
Double Quote |
Arrays are similar to variables, in that they are used to store pieces of data. However, unlike variables, arrays are useful because they can hold more than one piece of data at a time. For example, imagine that you wanted to store all the items in a shopping list. You could use an array to store these items. To create an array in PHP you use the array () function. Each item in the array is identified by a key . So to create an array for our shopping list, we could use the following code:
$shoppingList = array( 1 => "toothpaste", 2 => "sun cream", 3 => "
band
-aids");
To retrieve an element (a specific bit of data) from the array, we use the name of the array followed by the key in square brackets. So, to get the third item from our array, we can use the following piece of code:
echo "The third item in the shopping list is $shoppingList[3];"
This would display "The third item in the shopping list is band-aids" on the screen.
PHP uses a variety of control structures to affect which code is executed, when, and for how long. We'll cover two types here, if and foreach . There are other statements, like do. .while, which we won't cover here. More details and useful examples can be found in the online PHP manual.
If you've used any other programming language, then you'll be familiar with the if statement. It's used to conditionally execute some code: if a condition is met, then run a section of code.
For example, if we have two variables, $apples and $bananas , and we want to print a message on the screen if the value of $apples is greater than $bananas , we could use the following code:
if ($apples > $bananas) echo "You have more apples than bananas!";
Here the code checks to see whether a condition is met: whether $apples is greater than $bananas , using the > operator. If so, then the second line is executed, and the message is displayed. There are several conditional operators that you can use in your code:
|
Operator |
Meaning |
|---|---|
|
== |
Equal to |
|
!= |
Not equal to |
|
<> |
Not equal to |
|
< |
Less than |
|
> |
Greater than |
|
<= |
Less than or equal to |
|
>= |
Greater than or equal to |
Be careful when comparing two variables for equality. The correct operator to use is == (two equals signs). This is because = (one equals sign) is the assignment operator, for setting the value of variables.
If you want the if statement to conditionally execute more than one line of code, then you can enclose the block of code that you want to execute with curly braces, {} . So, for example, you could make the example we used above set the number of bananas to zero as well as displaying the message by using the following code:
if ($apples > $bananas) { echo "You have more apples than bananas, so I'm taking away your bananas!"; $bananas = 0; }
The
foreach
statement is used to loop through an array, looking at all of the elements in the array in
<?php $numbers = array(2,3,4,2); foreach ( $numbers as $v) { echo "Current value of \$v: $v <BR>"; } ?>
this would produce the following result:
Current value of $v: 2
Current value of $v: 3
Current value of $v: 4
Current value of $v: 2
You may have noticed the use of some of PHP's own functions in the code produced by Dreamweaver in previous chapters. They encapsulate a task that can then be called and run from elsewhere in your code.
It's possible to write your own functions in PHP and use them in a similar manner, allowing you to reuse a piece of useful code without constantly retyping it. For example, you could write the following function that checks the length of a certain piece of data:
<?php function check_length($data) { if (strlen($data) < 6) return "The data was too small"; else return "That data was fine"; } ?>
If the length of the data passed to the function as a parameter between its parentheses is less than 6, then the string "The data was too small" is returned by the function to wherever it was called from. If the length of the string wasn't less than 6, then the string "That data was fine" is returned.
The function can then be called
<?php $example = "qwertyuiop"; echo check_length($example); ?>
In this case, the data was fine is printed to the page, since the value "qwertyuiop" has more than 6 characters.
If you find that you need more information about the PHP commands we use in the hand coding examples in this chapter, you may find the following two web sites useful.
First, there is PHP.net - http://www.php.net/ . This is the home of PHP and contains a huge amount of useful information, including online PHP manuals, searchable by function.
Second, there is Zend -
http://www.zend.com/
. The Zend founders created PHP 4 and the Zend Engine, on which all PHP site and applications are run. Their web site contains many articles, code
When you're hand coding in PHP, it's very useful to have a browser
If you prefer to learn from a book, we recommend
Professional PHP4
(Harish Rawat et al., Wrox Press, ISBN 1-861006-91-8). This is an
Although Dreamweaver MX contains a range of built-in PHP server behaviors to perform various operations, if you're building a complex site, you'll soon find that you want to do something for which there isn't a built-in server behavior.
Since PHP is an open-source product, it is widely supported by a range of open-source developers and a huge
Hand coding allows you to implement the latest PHP developments in your code as soon as they are released. It offers you total freedom to ensure that your web site does exactly what you want it to, and you are not just limited to the built-in code generated by your development program.
Code that you've written yourself can also interact with the Dreamweaver MX-generated code, so you can create standard code quickly using the Dreamweaver MX server behaviors, and then modify it yourself to perform extra functions.