In this tutorial, we would learn the basic syntax of a PHP program.
- Escaping in PHP Code Block
- Comments in a PHP Code
- Case Sensitivity in PHP Code
- Statements, Expressions and Blocks
1. Escaping PHP Code Block
Since PHP can be embedded into a HTML page, we need a way to separate he PHP code from other page elements. You achieve this using a method called ‘escaping to PHP’. This can be achieved using four methods:
Canonical PHP Tags
This is the most common PHP tag as shown below
<?php .... .... <?
Short-Open Tags
This is simply a short form of the canonical tag and is given below:
<? .... <?
To enable PHP to recognize the short-open tags, you can do one of two things:
- choose enable-short-tags configuration option when building PHP
- set the short_open_tag setting in the php.ini file
ASP-style Tags
This is a kind of tag that mimics ASP.net code style. It looks like this:
<% .... <%
Similarly, for this to work, you also need to set this option in your php.ini file
HTML Script Tags
This is similar to the script tags we normally use when embedding JavaScript in HTML pages. It goes this way:
<script language = "PHP"> ... </script>
2. Comments in a PHP Code
A comment in programming is a piece of code that is ignored by the compiler or interpreter. It exists only to provide some readability to the program by humans. Comments are not included in the final results.
PHP supports two styles of comment formats:
Single-line comments – this style of comment is used for single line remarks. Some examples are given below:
<?php # An example of single-line comment # With a second line as you can see // You can also use this C++ style print 'A good example of single-line comments' ?>
Multi-line printing – in this style, the comments span two or more lines. Se examples below:
<? # Our 1st Example print <<<END This example the "here document" syntax to pring multiple lines with $variable interpolation. The here document terminator must appear on a line with just a semicolon and extra whitespace! END; # The 2nd Example print "This spans multiple lines terminated with semicolon. The newlines will be output as well"; ?>
Multi-line comments – They are generally used for providing a pseudocodelike explanation to the code. The multi-line comments in PHP is the same as multi-line comments in C and C++. An example is given here
<? /* This is an C++ type of multiple lines terminated with */ ?>
3. Case-Sensitivity in PHP
First note that PHP is case sensitive similar to Java. This means that $Value is not the same as $value.
Heres is an example to prove this
<html> <body> <?php $value = 38; print("Variable value is $capital<br>"); print("Variable Value is $CaPiTaL<br>"); ?> </body> </html>
Handling Whitespaces – PHP is whitespace insensitive. This means that whitespace characters does not matter to the PHP interpreter. These includes tabs and newlines.
4. Statements, Expressions and Blocks
Statements is PHP is terminated with a semicolon (;)
Expressions are a combination of tokens (letters, words, symbols etc)
Blocks of code are placed inside curly braces. See example below:
if (10 == 5 + 5)
print("I bet you, this is very correct!.<br>");
if (20 == 10 + 10) {
print("I bet you");
print("this is very correct<br>");
}