PHP Syntax

When talking about programming languages, syntax describes the orderly way in which symbols are to be combined for them to be considered properly structured in that language. PHP is no different.

PHP has various rules such as how a script is to be started and ended as well as how instructions in the script should be terminated.

When writing PHP scripts we need to bear in mind the syntax that PHP uses. Most of the PHP tutorials here at Tutorial Arena start by showing the correct syntax for the various elements which are examined.

Opening and closing PHP tags

All PHP scripts must start with:

<?php
 
 
//OR
 
 
<?

All PHP scripts should end with:

?>

The latter method of opening the PHP script (<?) is called the shorthand form and will only be recognized as starting the script if the server is specifically configured to do so. In general, one should use the former method (<?php) when writing scripts. It saves a lot of headache by preventing compatibility issues down the road if, for example, your script is run on a server which does not recognize the shorthand form of starting the script.

The semi-colon

All statements in PHP must end with a semi-colon. This is how the interpreter knows that the statement has ended and is useful as we will see later. Note that opening and closing braces are not considered statements (so they do not need a semi-colon). The majority of errors that a newcomer to PHP will face will probably be because they have inadvertently left out the semi-colons at the end of their statements.

<?php  //this is not a statement
 
    echo 'This is a statement and must end with a semi-colon';
 
?>

Whitespace

Whitespace refers to any spaces, tabs, indentation, and the like that is invisible on a white background. Whitespace is important as it allows us to structure our code in a way that can be easily read. As we learnt above, each statement is terminated by a semi-colon so as a result we can have any amount of whitespace between statements and it will not cause any problems.

<?php
 
    for ($i = 0; $i < 10; $i++)
    {
        echo "Tutorial Arena";
    }
 
?>
 
// IS THE SAME AS
 
<?php for ($i=0;$i<10;$i++){echo "Tutorial Arena";} ?>

As you can see, one is clearly easier to read. Take care to use whitespace generously when writing your code. It will make the code easier to read and this is helpful when debugging errors.

PHP Comments

Comments allow us to write text in our PHP code and have the PHP interpreter ignore them. This is useful since we may want to write our name at the top of the script, we may want to explain what a line of code does, or any other of a number of things. Regardless, of what you want to write as a comment, there are two types of comments in PHP that you can choose from. There is the single line comment and the multi-line comment.

The single line comment always terminates at the end of the same line which it started.

The multi-line comment will go over as many lines there are until the symbol that terminates it is reached.

Single-line PHP Comments

The single line comment begins with // or # and spans to the end of the same line.

<?php
 
    // everything after '//' to the end of the line is ignored.
    # echo 'This will not get executed';
    echo 'Tutorial Arena';
 
?>

Result:

Tutorial Arena

As we can see if we run the example code, the first sentence is ignored, and so is the first echo statement. Only the second echo statement gets executed since it is the only one that isn't "commented out".

Multi-line PHP Comments

The multi-line comment begins with a /* and is terminated with a */ and therefore can span multiple lines.

<?php
 
  /* everything after the beginning symbol to
     the ending symbol is treated as a comment
     and will be ignored by the PHP interpreter
 
     echo "First";
  */
 
     echo "Second";
?>

Result:

Second

As we can see, everything within /* and */ is ignored and only the second echo statement is executed.

Commenting PHP Code

Comments are very important when programming. They allow you to make notes about your code as you write it. They will also make your life easier. A single comment can explain what 10 confusing lines of code does and so it makes review and debugging easier. Persons who look on your code later (and even you yourself) will have an easier time reading it.

It is also sometimes useful to "comment out" (place comment symbols around) a piece of actual code to have it not executed, rather than erasing the line of code altogether. This helps tremendously when debugging since you can safely ignore a line without erasing it.

Comments are just as important as properly using whitespace when programming! The PHP tutorial was simple enough, however understanding these simple concepts will make you into a better programmer.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...