0% found this document useful (0 votes)
1 views

dN_PHPIntroduction_SyntaxAndStructure

The document provides an introduction to PHP, covering its syntax, file structure, and basic programming concepts. It explains the use of PHP tags, command separators, control structures, comments, variables, and constants, emphasizing their importance in web development. Additionally, it highlights the case sensitivity of variable names and the methods for defining constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

dN_PHPIntroduction_SyntaxAndStructure

The document provides an introduction to PHP, covering its syntax, file structure, and basic programming concepts. It explains the use of PHP tags, command separators, control structures, comments, variables, and constants, emphasizing their importance in web development. Additionally, it highlights the case sensitivity of variable names and the methods for defining constants.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PHP Introduction

Syntax and Structure


PHP syntax has some similarity to other ‘C’ type languages, such as Java or C#, but there
are also differences.

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 Basic Syntax

PHP Open and Close Tags


PHP script is ‘signalled’ in a file by an opening PHP tag and, if there is HTML in the file, a
closing PHP tag.

The PHP opening tag is <?php.

The PHP closing tag is ?>.

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;

However, for clarity, it is preferable to have statements on separate lines.


1> $name = "Fred";
2> $age = 78;
3> 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:

• a condition to be met to determine whether a section of code is executed or not ( if,


switch),
• an iteration over a section of code for a determined, or an undetermined, number of
times (for, foreach, while, do…while),
• a piece of code that is to be called when needed ( function, object)

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

Philip Windridge <[email protected]>


2
The variable $count is incremented and the message is output because the condition is true.
If the condition were false then the code in the statement group would be ignored.

Comments
Single line comments in PHP are denoted by the double forward slash //:
1> // This is a single line comment

Which can also appear at the end of a line of code:


1> if (true) { // Condition hard-coded for demonstration

A comment can also be denoted by the hash #:


1> if (true) { # Condition hard-coded for demonstration

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";

Line 2 does not overwrite the value of $name.

Assigning Values
Variables are assigned by value:

Philip Windridge <[email protected]>


3
1> $a = 1;
2> $b = $a;
3> $a += 1;

Line 1: the variable $a is assigned the value 1.

Line 2: the variable $b is assigned the value of $a.

Line 3: Incrementing the value of $a has no effect on the value of $b which remains as 1.

It is possible to assign by reference as in the following code:


1> $a = 1;
2> $b = &$a;
3> $a += 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 are also case-sensitive although there is an option to create a case-insensitive


constant when using the define()2 function.

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;

Capitalising the constant name is convention, it is not required.

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

Philip Windridge <[email protected]>


4

You might also like