PHP Syntax and Statements

In this tutorial, we will understand the basic PHP syntax and structures. Before we talk about the syntax of PHP, let us first what is syntax in any programming language.

A syntax in PHP or any other programming languages is a set of rules that must be followed to write properly program code so that machine can understand, interpret, and execute. In short, syntax is like the grammar of any programming language.

PHP’s syntax and semantics are similar to most other programming languages (C, Java, Perl) with the addition that all PHP code must be enclosed within the special tags.

The general syntax to write PHP code is as:

<?php
   // PHP code goes here.
?>

In the above syntax,

  • <?php is the opening PHP tag that begins with a less than symbol (<) followed by the question mark (?) and then comes the word “php”.
  • ?> is the closing PHP tag that starts with a question mark followed by a greater than (>) symbol. This PHP tag indicates the end of PHP code.

The opening tag <?php and the closing tag ?> are called PHP tags that tell the web server where the PHP code starts and ends. Any statement or text within these tags is interpreted as PHP code. Any statement outside these tags are considered as HTML code.

Note that PHP is a case sensitive language, which means that “VAR” is not the same as “var”. However, PHP tags are not case-sensitive, but it is strongly recommended that you use the lower-case letter.

Styles of PHP Syntax Tags


Actually, there are two styles of PHP tags: standard PHP tags and Shorthand PHP tags.

1. Standard PHP Tags

The above PHP syntax is a standard PHP tag. These are the most commonly used and recommended tags for embedding PHP code. All PHP configurations support these tags and they are the safest choice for guaranteeing compatibility for all servers. These tags are commonly used with XML (Extensible Markup Language) documents.

2. Shorthand PHP Tags

The general syntax of PHP using shorthand tags is as follows:

<?
   // Your PHP code here
?>

This is the shorthand version of the standard PHP tags that follow the style of a SGML (Standard Generalized Markup Language) processing instruction.

However, it is not recommended to use this due to compatibility issues, as it may be disabled in some server configurations. If you want to use this type of tag, you either enable the short_open_tag setting in the config file or compile PHP with short tags.

PHP Statements


A statement in PHP refers to a single line of code that performs a specific action such as assigning a value to a variable or calling a function. Each statement typically ends with a semicolon (;). For example:

$x = 5; // This is a statement
echo $x; // This is another statement

As you can see in the above code, there is a semicolon at the end of echo statement. However, you only write one statement in the block of code, you can omit the semicolon (;).

But if you have more than one statement, you must finish each line of statement with semicolon. In PHP, or any other programming languages like Java, the semicolon signifies the end of a statement.

Semicolon allows the PHP interpreter to separate one statement from another. Omitting the semicolon can lead to syntax errors unless the statement is the last one in a block or embedded in HTML.

For the sake of best practice, you must always end your statement (s) with a semicolon. PHP Statements can include various elements, such as expressions, assignments, function calls, and control structures.

You have known about the basic syntax of PHP as well as PHP statements with an example. I hope that you will have understood the basic syntax. Stay tuned with the next tutorial where you will understand how to write and run your first PHP program in VS code editor.