1. What Is Basic PHP Syntax?
Usually, there are some definite rules and principles for PHP syntax that we must follow in order to write the correct script. These rules set the foundation of the PHP syntax and structure, and guide the developers to write the code accordingly. On contrary to it, if a script violates the syntax guidelines, there will be no results or output of all written PHP scripts, and instead, we will get error or warning on the screen.
Hence, PHP syntax obeying is a must and the first rule for beginners as well as experts as it is a strictly typed language. Follow along with this comprehensive PHP tutorial to learn the basics of syntax and gain in-depth knowledge of syntax types and rules to follow.
1.1. Basic PHP Syntax
The next example describes a simple PHP code following the syntax rules and principles.
Syntax Example
<?php [------- Content Goes Here -------] ?>
2. What are PHP Structure Basis and Syntax Types?
2.1. What are PHP Delimiters
- Firstly, the parser interprets the code only if it lies within the delimiters.
- Further, the delimiters serve as boundaries for the code and confine the script.
- Importantly, the PHP code should not go outside this limit otherwise the server will neglect this code and will not parse that script, which is out of the boundary.
- Therefore, these delimiters must be considered the final authority.
- In PHP, there are several syntax types of these delimiters.
These delimiters are listed below:
2.1.1. Canonical PHP Tags or Long Tags
This is the first delimiter and is common in practice.
Canonical Tags
<?php [------- Content Goes Here -------] ?>
2.1.2. Short Tags
The second PHP delimiter has little difference from the first delimiter.
Short Tags
<?= [------- Content Goes Here -------] ?>
2.2. Non-Recommended PHP Delimiters
There are specific PHP delimiters or tags which are deprecated and are no further recommended.
- HTML Script Tags
- SGML Short Tags
- ASP Type Tags
2.2.1. HTML script Tag
It uses a "language" attribute. Also, this tag is removed in PHP 7.
HTML Script Tags
<script language="php"> [------ Content Goes Here ------[ </script>
2.2.2. SGML Tag or Short Opening Sag
SGML Short Tags
<? [------- Content Goes Here -------] ?>
2.2.3. ASP Style Tags
This ASP style tag is removed in PHP 7.
ASP Type Tags
<% [------- Content Goes Here -------] %>
3. PHP Syntax Rules and Guidelines
Basically, there are some guidelines and rules, which we must follow, in order to write the correct syntax of this language. These sets of syntax rules are a must, and any PHP developer should learn them as basics. Below is the list of all the rules:
- Basic language constructs
- The Delimiter
- Delimiter Exception
- Is it a case-sensitive Language?
- White Spaces
3.1. 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.
- Variables: The PHP variables MUST be prefixed with a $ sign
- 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()
3.2. PHP Semi-Colon (;)
Most importantly, PHP is nothing without a semi-colon(;). It means that the statements must end with a semi-colon(;) otherwise, there will be a syntax error. The below script will show a syntax error because there is no "semi-colon(;)" at the end of the statement, after variable $name.
Semi-colon(;) Needed
<?php $name = 'Tuts Insider'; $domain = 'www.tutsinsider.com'; echo 'The name of this website is '. $name echo 'The domain of the website is'. $domain; ?>
3.3. PHP Semi-Colon (;) Exception
However, there is one exception in the case of the semi-colon(;). The semi-colon(;) is a must if there is no ending delimiter ?> after the last statement. But if there is an ending delimiter after the last statement, then a semi-colon is not necessary. The below PHP code is permitted, as there is a delimiter after the end of the statement, regardless of the absence of a semi-colon.
Semi-colon(;) Exception
<?php $name = 'TutsInsider'; echo 'The name of this website is ' . $name . '.' ?>
3.4. 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 identical. However, only variables show case-sensitive behavior, so it is said that PHP is a case-sensitive language. Consider the below example:
Example
<?php function Hello(){ echo "Function <strong>name</strong> is not case sensitive.<br>"; } hELLO(); FuncTION World(){ echo "function <strong>keyword</strong> is not case sensitive.<br>"; } World(); $tag = "Learn PHP with "; $domain = "www.tutsinsider.com"; echo $tag . $domain . "<br>"; echo $tag . $Domain; ?>
The above code will have the following output after interpreting. This output clearly depicts the case sensitivity of PHP function names, keywords, and variables.
It is vivid that, keywords and function names are not case-sensitive for PHP. However, only variables show case-sensitive behavior as variable "$domain" has no output when it has capital D as "$Domain".
Output
3.5. White Spaces
Furthermore, PHP is not sensitive to white spaces as well, therefore, the parser will neglect any number of white spaces that are in the code. However, developers may put white spaces for making PHP script more readable. Thus, PHP language is in-sensitive for white spaces.
4. PHP Standard Recommendations (PSR)
Initially, this programming language was a bit disorganized, therefore, the PHP Framework Interloop Group (FIG) made some standard recommendations for developers in 2009. Hence, these PHP standard recommendations (PSR) have set a roadmap for 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 the remaining are drafted for approval. The subsequent table displays the 20 PSRs with their recommenders.
PSR No | 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 |
PSR No | PSR Name | Editor |
5 | PHPDoc Standard | Chuck Burgess |
19 | PHPDoc tags | Chuck Burgess |
PSR No | PSR Name | Editor |
8 | Huggable Interface | Larry Garfield |
9 | Security Advisories | Michael Hess |
10 | Security Reporting Process | Michael Hess |
PSR No | PSR Name | Editor |
0 | Autoloading Standard | Matthew Weier O’Phinney |
2 | Coding Style Guide | Paul M. Jones |