Quick Reach
The Syntax
In this chapter, you will be given a basic idea of PHP syntax that will help you developing PHP foundation.
Escaping to PHP
The web pages usually contain a mix of code. The PHP parser needs a way to differentiate PHP code from the other elements. This mechanism is called as ‘escaping to PHP‘. There are four methods of applying this:
Canonical PHP tags
This the most commonly used method that uses:
1
|
<?php...?>
|
The whole PHP code is placed in between:
opening tag <?php and the closing tag ?>
Short-open (SGML-style) tags
Short or short-open tags look like this:
1
|
<?...?>
|
This is the shortest tags but is not recommended way. If your web page is using XML, the same syntax is used for this as well. To use this method you have to do two things to let PHP recognize these tags.
When you are building PHP, choose the –enable-short-tags configuration option.
Open your php.ini file and set the short_open_tag ON. This option must be disabled to parse XML with PHP since the similar syntax is used for XML tags.
ASP-style tags
ASP-style tags are used by Active Server Pages to delineate code blocks. The ASP-style tags look like this:
1
|
<%...%>
|
You have to set the configuration options in php.ini file in order to use the ASP-style tags.
The HTML like script tags
HTML script tags look like this:
1
|
<script language="PHP">...</script>
|
Commenting PHP Code
The Comments are the lines inside your code pages that parser does not execute. This is for the developer reading purpose where the purpose of the code/page etc is mentioned.
The two ways you may comment in PHP code files are as follows:
Single-line comments
Single line comments are usually used for short details or notes that are related to the local code. Examples of this are:
1
2
3
4
5
6
7
8
9
10
11
|
<?
# This is a comment, and
# This is the second line of the comment
// This is a comment too. Each style comments only
print "An example with single line comments";
?>
|
Multi-line commenting
The Multi-line commenting is normally used to provide pseudocode algorithms and detailed information when required. This style of commenting is same as used in the C Language.
An example of multi-line commenting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?
/* This is a comment with multiline
Author : Author Name
Purpose: Multiline Comments Demo
Subject: PHP
*/
print "An example with multi line comments";
?>
|
PHP is whitespace insensitive
Whitespace is the stuff you type that is typically invisible on the screen, including spaces, tabs, and carriage returns (end-of-line characters).
The whitespace is considered as the character. PHP is insensitive in the case of whitespaces. That means if you have spaces, tabs, and carriage returns (end-of-line characters). It will not affect PHP execution. For example, the following lines will give the same output:
1
2
3
4
5
6
7
8
9
|
$five = 2 + 3; // single spaces
$five <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$fine =
2+
2; // multiple lines
|
PHP is case sensitive language
See the following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<html>
<body>
<?
$casesensitive = 67;
print("Variable casesensitive is $casesensitive<br>");
print("Variable CaseSensitive is $CaseSensitive<br>");
?>
</body>
</html>
|
This will produce the following result:
Variable casesensitive is 67
Variable CaseSensitive is
PHP statements are terminated by semicolons
A statement in PHP is any expression which is followed by a semicolon (;).
Any sequence of correct PHP statements that is enclosed by the PHP tags is a valid PHP program. See example below:
1
$greetings = "Welcome to PHP!";
Braces make blocks
The curly braces {} are used to enclose multiple statements like in the following example.
Although the statements cannot be combined like expressions, you can always put a sequence of statements anywhere a statement can go by enclosing them in a set of curly braces.
Here both statements are equivalent:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
if (3 == 2 + 1)
print("This - is curley braces example");
if (3 == 2 + 1)
{
print("This - is curley");
print(" braces example.<br>");
}
|
Details of if else statement are in the coming chapters.
Until now you should have a basic taste of the PHP syntax. Next chapter will take you through the Variable Types in PHP.
Leave A Comment?