Learn basics of PHP syntax

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:

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:

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:

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:

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:

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:

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:

PHP is case sensitive language

See the following example:

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:

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:

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.