Every programming language/scripting language/markup language has its own unique syntax. Every developer uses those syntaxes while developing a web application or desktop application or mobile application. Like all other languages, PHP has its own unique syntax. So at the beginning of the tutorial series, we should learn first about the PHP syntax and how they work.
PHP Syntax Overview
Now we will learn the basic syntax used in PHP scripting language. A PHP file ends with a .php file extension. You can create a PHP file through any text editor (Don’t confuse with text processor) such as Notepad++, Sublime text editor, etc. A PHP file contains PHP code that is written by following the program syntax. We will discuss those syntaxes in the next section.
In the image above, you can see a PHP file ends with .php and the file name is
index.php. And inside the file, you can see some PHP code that starts with
<?php and ends with
?> , Inside these two tags the PHP code is written. In the later chapter, we will learn in detail how to write those codes and make a complete web application and functionality.
Now we will talk about PHP syntax. PHP has some defined syntax that every developer must have to use while defining codes. The most basic syntax discussed in the below sections:
1. Start tag & End tag in PHP
By default inside a PHP file the codes start with a specific tag and in the same way, end with a specific tag. But for PHP scripting language the used tag can vary means you have some option to use this code. Let’s talk about that.
1.Standard PHP Tags: When PHP code starts with
<?php and ends with
?> tags. Both tags called as standard tags. It is also called as canonical tags. Most of the time PHP developers refer to use the standard or canonical tag for a PHP file. Example:
<?php # Here echo command is used to print echo "Hello, world!"; ?>
2. Short HTML Tags: To initialize a PHP code script you can also use a shorten tag than the canonical or standard tags. When the scripts initialize with
<? and end with
?> is called Short HMTL Tags. It does not affect your PHP code. But all the developers and community recommend using the standard format. But one thing, in the PHP configuration file called php.in a setting, must be turned on to work this Short HTML Tags feature. If it is disabled, to turn this feature on, open the php.ini file and search for “short_open_tag” then turn the option “on” from “off”. Example of PHP script using Short HTML Tags:
<? # Here echo command is used to print echo "Hello, world!"; ?>
3. ASP Tags style: You can also initialize a PHP script with
<% and terminate the code with
%> . But to use this feature you must have to configure the necessary thing in
php.ini in your Apache server. This feature is actually used when you will work with ASP (Active Server Page).
4. HTML Tag script: You can directly embed the PHP code in an HTML file with HTML like tags. See Example below:
<script language="php"> #It is discontinuted from PHP version 7.0.0 <script language="php"> echo "hello world!"; </script>
Note: This implementation is no longer supported from PHP version 7.0.0
2. Comments Implementation in PHP
Comments are the most common feature in a programming language. Every programming language has a commenting syntax. But what are comments in a programming language? In PHP, a comment is actually some notes in the program source code which are ignored or read by PHP engine. Then the question arises if the comment is ignored by the PHP engine then what’s the need of comment in PHP? When a programmer writes a complex code that difficult to understand at a later time to the code writer or other programmers. To avoid this problem, comments are necessary to describe the difficulty means programmers notes what is done in the complex code by human language.
In PHP, you can define comments in two ways (e:g: Single line comment and multi-line comment). Let’s discuss those in details:
1.Single Line Comment: As the name, the single-line comment is written in a single line. This line number can’t be extended to more. Basically, this comment is used when we need to write a small note that is not extended to one more line. To add a single comment we need to add a double slash (//) or single hash (#). Let’s see an example:
<?php // It is a single line comment // this kind of comment can't be extended to more lines echo "Hi, this is a very funny idea!"; # It is also a single line comment. ?>
Hi, this is a very funny idea!
2.Multi-line comment: When programmers need a descriptive note to understand a complex code then the multi-line comment is used. To imply the multi-line comment we have to start the comment with
/* and end with
*/. See Example below:
<?php /* It is multiline comment and this section is not executed programmers use this for descriptive note */ $var1 = "hello world!"; echo $var1; ?>