1. What Is Basic PHP Syntax?

Usually, there are definite rules for PHP syntax that need to be followed. These rules set the foundation of the PHP or PHP 7 syntax and guide the developers to write PHP code accordingly. On contrary to it, if PHP code violates the syntax guidelines, there will be no results or output of all written PHP script. Hence, PHP syntax obeying is a must and the first rule for beginners as well as experts. Follow along this comprehensive PHP tutorial to gain in-depth knowledge of PHP syntax.

1.1. Basic PHP or PHP 7 Syntax

The succeeding example describes a simple PHP code following PHP syntax rules.

Example

<?php
  [——- Content Goes Here ——-]
?>

Tutorial Contents:

  1. What are the Basics of PHP Syntax?
  2. Structure and Rules to Define PHP Syntax
  3. PHP Standard Recommendations (PSR)

2. What are PHP Structure Basis and Syntax Rules?

2.1. PHP Delimiters

Firstly, PHP interprets the PHP code only if it lies within PHP delimiters. PHP delimiters serve as boundaries for PHP code. The PHP code should not go outside this limit otherwise PHP neglects this code. Therefore, PHP delimiters must be considered as the final authority.
Furthermore, there are two types of PHP delimiters. These delimiters are listed below:

2.1.1. Canonical PHP Tags or Long Tags

This is the first PHP delimiter and is common in practice.

<?php
  [——- Content Goes Here ——-]
?>

2.1.2. Short Tags

The second PHP delimiter has little difference from the first delimiter.

<?=
  [——- Content Goes Here ——-]
?>

2.2. Non-Recommended PHP Delimiters

There are certain PHP delimiters or tags which are deprecated and are no further recommended.

2.2.1. HTML script Tag

It uses a “language” attribute. Also, this tag is removed in PHP 7.

<script language=”php”>
  [—— Content Goes Here ——[
</script>

2.2.2. SGML Tag or Short Opening Sag

<?
  [——- Content Goes Here ——-]
?>

2.2.3. ASP Style Tags

This ASP style tag is removed in PHP 7.

<%
  [——- Content Goes Here ——-]
%>

2.3. PHP Basic Language Constructs

Generally, PHP was extended from C Language, with exceptions and enhancements for use in web development. Moreover, the PHP code must be in UTF-8 character encoding. Let’s have a look at PHP language constructs.

  • PHP Variables: The PHP variables MUST be prefixed with a $ sign
  • PHP Classes: PHP class names MUST be capitalized like MyClass
  • Class Constants: Class constants MUST be in an upper case like COUNTRY_NAME
  • Methods: PHP methods must be Capitalized like CountryName()

2.4. PHP Semi-Colon (;)

Most importantly, PHP is nothing without a semi-colon(;). It means that PHP code statements must end with a semi-colon(;) otherwise, there will be a syntax-error. The below PHP code will show a syntax-error because there is no “semi-colon(;)” at the end of the statment, after variable $name.

Example

Missing Semi-Colon (;)
<?php
$name = ‘Tuts Insider’;
$domain = ‘www.tutsinsider.com’;
echo ‘The name of this website is ‘. $name
echo ‘The domain of the website is’. $domain;
?>

Output

syntax error, unexpected end of file, expecting ‘,’ or ‘;’

2.5. PHP Semi-Colon (;) Exception

However, there is one exception in case of semi-colon(;). The semi-colon(;) is must, if there is no ending delimiter ?> after the last statemetn. But if there is an ending delimiter after the last statement, then semi-colon is not necessary. The below PHP code is permitted, as there is a delimiter after the end of the statment, regardless of absence of semi-colon.

Example

Semi-Colon (;) omitted at the end of the code
<?php
$name = ‘Tuts Insider’;
echo ‘The name of this website is ‘. $name
?>

2.6. Is PHP a Case Sensitive Language?

Normally, all the functions, class names and keywords are not case sensitive. Therefore, all the cases for keywords, class names and functions will be considered as identical. However, only variables show case-sensitive behaviour, so it is said that PHP is case-sensitive language. Consider the below example:

Example

<?php
FunCTION Hello(){
  echo “PHP Function name is case sensitive.”;
}
hELLO();

function tutsPie(){
  echo “PHP function keyword is case sensitive.”;
}
tutsPie();

$name = “Tuts Insider”;
$domain = “www.tutsinsider.com”;

echo $name . “owns” . $domain;
echo $name . “owns” . $Domain;

?>

The above code will have following output after interpreting. This output clearly depicts the case sensitivity of PHP function names, keywords and variables.
It is vivid that, keywords, function names are not case sensitive for PHP. However, only variables show case sensitive behaviour as variable “$domain” has no output when it has capital D as “$Domain”.

Output

Hello() Function Output

PHP Function name is not case-sensitive.

tutsPie() Function Output

PHP function keyword is not case-sensitive.

Variables Output

Tuts Insider owns www.tutsinsider.com
Tuts Insider owns

2.7. White Spaces

Furthermore, PHP is not sensitive for white spaces aswell, Therefore, PHP will neglect any number of white spaces that are in PHP code. However developers may put white spaces for making PHP code more readable. Thus, PHP code is in-sensitive for white-spaces.

3. PHP Standard Recommendations (PSR)

Initially, PHP coding was a bit disorganized. Therefore, the PHP Framework Interloop Group (FIG) made some standard recommendations for the developers in 2009. Hence, these PHP standard recommendations (PSR) has set a roadmap for the individuals and organizations in their development journey.
Moreover, there are 20 PHP standard recommendations (PSRs). However, some of them are accepted, some are rejected whereas remaining are drafted for approval. The subsequent table displays the 20 PSRs with their recommenders.

Accepted
PSR Number PSR Name Editor
1 Basic Coding Standard Paul M. Jones
3 Logger Interface Jordi Boggiano
4 Autoloading Standard Paul M. Jones
6 Caching Interface Larry Garfield
7 HTTP Message Interface Matthew Weier O’Phinney
11 Container Interface Matthieu Napoli, David Négrier
12 Extended Coding Style Guide Korvin Szanto
13 Hypermedia Links Larry Garfield
14 Event Dispatcher Larry Garfield
15 HTTP Handlers Woody Gilk
16 Simple Cache Paul Dragoonis
17 HTTP Factories Woody Gilk
18 HTTP Client Tobias Nyholm
Draft
PSR Number PSR Name Editor
5 PHPDoc Standard Chuck Burgess
19 PHPDoc tags Chuck Burgess
Abandoned
PSR Number PSR Name Editor
8 Huggable Interface Larry Garfield
9 Security Advisories Michael Hess
10 Security Reporting Process Michael Hess
Deprecated
PSR Number PSR Name Editor
0 Autoloading Standard Matthew Weier O’Phinney
2 Coding Style Guide Paul M. Jones
Give Us Your Feedback
OR
If You Need Any Help!
Contact Us