dN_PHPIntroduction_SyntaxAndStructure
dN_PHPIntroduction_SyntaxAndStructure
PHP is very well suited to web development. It can be used with HTML in the same file or it
can be placed in a separate file. In web development projects it is usually both.
PHP Files
All PHP files should have the .php extension.
On a web server, the .php extension means that the contents can be passed to the PHP
processor so that any PHP can be executed when forming the server’s response to the
client.
PHP code is written between the opening and closing tags. However, if the file just contains
PHP then it is preferable to only use the opening tag at the very beginning of the file:
1> <?php
The closing tag is required when the file contains more than just PHP. The following is a
simple example showing PHP embedded in HTML. It demonstrates the use of both the
opening and closing tags.
1 <?php
2> $msg = "Hello World!";
3> ?>
4> <!DOCTYPE html>
5> <html lang="en">
6> <head>
7> <meta charset="UTF-8">
8> <title>Embedded PHP</title>
9> </head>
10> <body>
11> <p>
12> <?php echo $msg; ?>
13> </p>
14> </body>
15> </html>
[link to code]
The code above assigns a string value to the $msg (line 2) which is then output (line 12).
Note that the opening and closing tags on lines 1 and 3 are on separate lines. This could
also be written on just one line, as is the case on line 12.
Command Separator
All statements in PHP should end with the semi-colon, just as in Java and C#. Not doing so
will generally cause an error.
Signalling that the statement is complete with the semi-colon means that it is possible to
have more than one statement on one line:
1> $name = "Fred"; $age = 78; echo $name, " is ", $age;
Control Structures
Control structures1 are one or more code statements that need to be treated as a group. For
example, there may be:
Code which is to be treated as part of a group is usually bounded by curly braces “{…}”.
1> if (true) {
2> $count++;
3> echo "True condition executed.";
4> }
1 http://www.php.net/manual/en/control-structures.intro.php
Comments
Single line comments in PHP are denoted by the double forward slash //:
1> // This is a single line comment
Where a comment spans multiple lines then the comment can be enclosed by forward-slash
asterisk, asterisk forward-slash /*…*/:
1> /* A multiline comment that can appear
2> over a number of lines */
Although it is a common practice for multi-line comments to take the following form:
1> /*
2> * A multiline comment that can appear
3> * over a number of lines
4> */
Variables
A variable holds a value that can change as your program runs.
Variable Names
Variables are identified by having a dollar sign $ as the first character. This must
then be immediately followed by a letter [a-zA-Z] or an underscore _. This is then followed
by any number of letters, numbers or underscores but no whitespaces.
Variable names are case-sensitive. The following two variables are distinct for PHP:
1> $name = "Fred";
2> $Name = "Frank";
Assigning Values
Variables are assigned by value:
Line 3: Incrementing the value of $a has no effect on the value of $b which remains as 1.
The ampersand “&” immediately preceding the variable $a on line two assigns the value of
$a to $b by reference. Incrementing $a on line 3 now effectively increments the value of $b.
Constants
A constant holds a value that cannot be changed during runtime.
The name of a constant follows the same rules as names for variables but does not start
with the dollar sign $. It must have a letter or underscore [a-zA-Z_] as the first character
followed by any number of letters, numbers or underscores but no whitespaces.
Constants can be defined in two ways: using the define() function or using the const
keyword. The latter method is valid from PHP version 5.3.
1> define("KILOMETRE_CONVERSION", 1.6);
2> const CENTIMETRE_CONVERSION = 2.54;
Creating constants using the define() function can be done anywhere. However, the const
keyword cannot be used within functions, loops, if statements or try…catch blocks.
2 http://uk1.php.net/manual/en/function.define.php