Comments and Instruction Separator in PHP

Comments in PHP

Like all programming languages, PHP also have ability to create comments.

Comments are integral part of PHP programming which is not displayed by PHP parsar. Comments are like little secret between programmers and PHP compiler.

Why to use comments?

Comments will help you to remember what the code is for. It will help other programmers to understand your code.

Comments also allow you to debug your code. Sometimes you need to comment out some code and later uncomment it to check what it is actually doing.

How to define comments in PHP?

Comments can be single line comments or multi-line comments.

To define single line comment use // or #

To define multi-line comment use /* ... */. Comment content will be between the two astrick.

Example of single line comment:

# This is a comment
// This is another way of writing comment
echo "Comments will not be shown on executing this script";

Example of Multi-line comment:

/* This is a multi-line comment
   and can be of any number of lines */
echo "Hope comments will help you in your coding journey";

Make a habit of writing precise comments that are not too long or not too short.

Instruction Separator in PHP

Two or more instructions are separated by a semi colon (;) in PHP. But you can skip the semi color in your last instruction.

Here are your examples.

echo "hello";
echo "world";

#checkout that the last instruction is not followed by any semi-colon
echo "bye"

One more important thing. If you are having php script at the end of you code then you can also drop the closing php tag (?>).

It is useful at some places. But keep in mind if you are dropping the php closing tag then your last php instruction should end up with a semi-colon.


#why don't you remove the semi-colon and see what happens
echo "oh crap! where's the php ending tag!!!";
<< Displaying Output Variables and Data Types in PHP >>