In this lesson, you will learn about the basic syntax in PHP, PHP opening and closing tags, Canonical PHP tags, Short-open tags, and more, along with examples to better understand the topics.
Let’s examine a small application to show how PHP script syntax works. A statement in PHP is valid if followed by a semi-colon ;
and enclosed by the PHP tags; otherwise, PHP gives Parse error: syntax error message.
<?php echo "PHP Valid Statement"; ?>
<php
;
?>
The PHP parsing engine distinguishes PHP code from elements on the webpage written in other computer languages. “Escaping to PHP” or the “PHP delimiter” is the name of the differentiating procedure.
Enclosed are code blocks like <?php
and ?>
are recognized as PHP delimiters by the PHP engine and treated as PHP code rather than ordinary HTML. In PHP, there are four opening and closing tag pairs available.
Example
Canonical PHP tags: <?php
… ?>
Short-open tags: <?
… ?>
If you are writing your PHP script using Canonical PHP labels, you may be confident that the PHP parsing will continuously translate your labels legitimately. The PHP tag looks like this “<?php
… ?>
“.
Example
<?php echo "PHP tags"; ?>
Output
PHP tags
The format for short php tags is the question park ?. The user may believe that short-open tags (SGML-style) are the briefest alternative. However, PHP needs one or two things to recognize the tags.
When generating PHP, select the “—enable-short-tags” configuration option. First, enable the short open tag option in the PHP.ini file. Second, turn this option off when parsing XML with PHP because XML tags express similarly.
Example
<? echo "PHP tags"; ?>
Output
PHP tags
As XML tags follow, the ASP-style tags appear as follows: <%
… %>
. It mimics the style of Active Server Pages(ASP) tags to specify code blocks. So, you can use the same method to set up the option in your PHP.ini file if you want to use ASP-like tags.
Example
<% echo "ASP-style tags look like"; %>
Output
ASP-style tags look like
HTML script tags style like this:
<script language="PHP"> ... </script>
Example
<head> <script language="PHP"> echo "HTML script tags style looks like."; </script> </head>
Output
HTML script tags style looks like.
<script language="PHP"><script>
and ASP Labels are not declared anymore. These two labels and illustrations indicate as they were for reference reasons. They are deprecated and not in use as wellLike in C or Perl, every statement in PHP is terminated with a semi-colon ;
Example
<!DOCTYPE html> <html> <body> <?php //Simple program prints text echo "Separation of statements in PHP is a useful feature <br>"; echo "Do it with simple Semi-colon"; ?> </body> </html>
Output
Separation of statements in PHP is a useful feature Do it with simple Semi-colon
Also, You don’t need to embed HTML tags in a PHP file.
Example
<?php //Simple program prints text echo "The separation of statements in PHP is a valuable feature<br>"; echo "Do it with a simple Semi-colon"; ?>
Output
The separation of statements in PHP is a valuable feature Do it with a simple Semi-colon
Furthermore, You don’t have to use a semi-colon in the last line of a PHP block because PHP does it for you.
Example
<?PHP echo " The semi-colon is not required in the last line of a PHP block; PHP automatically inserts one" ?>
Output
The semi-colon is not required in the last line of a PHP block; PHP automatically inserts one.
Although only one line is in the above PHP block, it is also the last line of the PHP block. So there is no need for a semi-colon.
Example
<?PHP //Simple program prints multiple line texts with a semi-colon echo "Although only one line is in the above PHP block, it"; echo "is also the last line of the PHP block. So there is "; echo "no need for a semi-colon." ?>
Output
Although only one line is in the above PHP block, it is also the last line of the PHP block. So there is no need for a semi-colon.
Three PHP statements are in the above code, with the Third and last statements automatically implying the end. In PHP programming, statements do not require a semi-colon ;
at the end.
This concludes the PHP Program Syntax lesson. In the next class, you will learn about echo and print statements in PHP.