Hands-on with PHP — Syntax

< Home < PHP Development Environment

Code of Coding

Every language has to follow a set of grammatical rules only then can a statement constructed in that language be interpreted and understood correctly, the same goes for any programming languages. Unlike human beings who can second-guess a poor grammatically formed statement, However, computers are inflexible machines that understand what you type only if you type it in the exact form that the computers expect. This expected form is called the syntax of the programming language, I coin it Code of Coding.

Unlike the grammatical complexity of human languages, The good news is that there aren’t that many syntax rules in PHP. I have categorized them into four groups:

PHP Tags

PHP code must be enclosed in a pair of <?php and ?> delimiters called PHP tags. Each pair of PHP tags tell the PHP parser where to start and stop interpreting the PHP code inside the tags. These PHP tags also allow PHP code to be embedded any way in a file, alongside HTML markup, CSS, and JavaScript.

Check out the example in the PHP Tags code section using the PhpFiddle widget.

<?php
echo "PHP code must be enclosed in a pair of &lt;?php and ?&gt; delimiters called PHP tags.";
?>

Return to Code of Coding

Terminating Semicolon

PHP statements must be terminated with a semicolon that acts as a separator between them. However, there is an exception as stated below:

The terminating semicolon of the last statement in a PHP code block can be omitted if the closing PHP tag is present.

Check out the example in the Semicolon Exception code section using the PhpFiddle widget.

<?php 
echo "This PHP statement is terminated with a semicolon.<br>";
echo "This PHP statement can omit the terminating semicolon since the closing PHP tag is present."
?>

Return to Code of Coding

PHP Comments

It is a well-known fact that reading, interpreting, and maintaining the source code of a computer program is an arduous undertaking. To alleviate the pain, programmers are taught to insert human-readable comments in between lines of code. These comments, written in a natural langauge and concise manner, serve to explain or clarify what a particular code is doing to a human reader, usually a programmer. These comments are excluded from the execution process of a computer programs and therefore have no effect on the outcome. Apart from the origin objective of code explanation or clarification, programmers sometimes use comments to temporarily leave out parts of a code during a debugging process as a means to narrow down the source of an error. What a clever hack!

There are two ways to insert comments into PHP scripts &mdash; single-line style or multi-line style.

Single-Line Comments

As shown in the Single-Line Comments code section, a single-line comment starts with either a pair of forward slashes // or a hash sign #, and continues until the end of the line. A Single-line comment is usually placed before the PHP statement that it seeks to explain or clarify. If a PHP statement and its comment are both short, then the comment may be placed behind the PHP statement on the same line.

Check out the example in the Single-Line Comments code section using the PhpFiddle widget.

<?php
// A single-line comment starts with two forward slashes //
echo "A single-line comment starts with two forward slashes //.<br>";

# A single-line comment starts with a hash # symbol
echo "A single-line comment starts with a hash # symbol.";
?>

Multi-Line Comments

A multi-line comment, on the other hand, can span multiple lines and is placed inside a pair of /* and */ delimiters as shown in the Multi-Line Comments code section.

Check out the example in the Multi-Line Comments code section using the PhpFiddle widget.

<?php
/*=============================
  Title : PHP Comments 
  Author: Peter Leow 
 =============================*/

/* 
  A user-defined function to calculate sum of two numbers:
  1. Get first number
  2. Get seconed number
  3. set result = first number + second number
  4. return result      
*/
function sum($x, $y) {
  $z = $x + $y;
  return $z;
}

$firstNumber = 2;
$secondNumber = 3;
echo "The sum of $firstNumber and $secondNumber is ", sum($firstNumber, $secondNumber);
?>

A PHP file may start with a multi-line comment that describes the meta information about the file, such as the title, authors, purpose, copyright statement, etc. More often than not, multi-line comments are placed before a user-defined function to explain the purpose and working of that function.

Return to Code of Coding

PHP Case Sensitivity

In PHP, all keywords and constructs (e.g. if, else, while, for, echo, print, etc.), classes, class methods, built-in functions, and user-defined functions are NOT case-sensitive, and are more commonly written in lowercase. On the other hand, variables, constants, array keys, class properties, and class constants are case sensitive.

Check out the example in the PHP Case Sensitivity using the PhpFiddle widget.

<?php
// ECHO is not case sensitive
ECHO "ECHO is the same as echo.<br>";

// $name is not the same variable as $NAME
$name = "Peter Leow";
echo $NAME;
?>

Notice that the first echo statement outputted successfully since ECHO is the same as echo thanks to case insensitivity, but the second echo statement resulted in an error message citing Undefined variable $NAME or words to that effect on the browser since $name and $NAME are two different variables owing to case sensitivity.

Consequences of Breaking Code of Coding

Mistyping a PHP keyword or breaking any of the syntax rules mentioned in the preceding sections will result in some form of error and the execution of the program will be halted.

In each of the following code sections, copy and run the code in the PhpFiddle widget, interpret the error messages, correct the mistakes, and run the code again. Repeat the process until it is error-free.

<?php
ech0 "What wrong with me?";
?>
<?php
echo "What wrong with me?"
<?php
/* What wrong with this comment?
?>

Return to Code of Coding

Quiz

Which one of the following expressions is NOT a valid PHP comment?
/# PHP comment #/
Correct!
# PHP comment
Wrong!
// PHP comment
Wrong!
/* PHP comment */
Wrong!
Which one of the following pairs is valid PHP tags?
<?php and ?>
Correct!
<? and ?>
Wrong!
<php> and </php>
Wrong!
<?php  and  ?php>
Wrong!
Are PHP keywords case sensitive?
False
Correct!
True
Wrong!
Which one of the following statements is true?
The terminating semicolon of the last statement in a PHP code block can be omitted if the closing PHP tag is present.
Correct!
If a PHP code block is located at the end of a PHP file, the PHP closing tag of this code block and the terminating semicolon of the last statement in this code block can both be omitted.
Wrong!

< Home < PHP Development Environment

Tagged with:

Speak Your Mind