PHP Basic Syntax

php basic syntax

When a user visits a PHP page (that ends with .php extension), the browser requests the page to the web server. The web server forwards the request to the PHP interpreter. PHP interpreter understands PHP syntax. It reads the file, and processes it to generate HTML document. Here you’ll learn the basic syntax used in PHP.

A simple PHP script

At first let’s take a look at a simple HTML script. If you know HTML (which is recommended before learning PHP), you’ll find it familiar.

<! DOCTYPE html>

        <meta charset="”utf-8”" />


         Hello World
      

If you view the above file in a browser, you’ll find the following output.

Output:
Hello World

To display the same output in PHP, PHP code (line 8 to 10) would be-

<! DOCTYPE html>

        <meta charset="”utf-8”" />


         <!--?php <br ?-->             echo “Hello World”;
         ?>
      

Now let’s examine the above PHP code (line 8-10) in detail.

PHP tags

All the PHP codes are written between starting and closing PHP tags. Starting and closing tags pair can be written in four ways-

  1. <?php ?>: All PHP codes must be written within <?php and ?> tags. In the above example, the PHP code in line no. 9 is placed within <?php and ?> tags.
  2. <script language=”php”> </script>: PHP codes written within <script language=”php”> and </script>.
  3. <? ?>: This is the shorter version of (<?php and ?>). But it is not recommended to use unless it is enabled in php.ini configuration file.
  4. <% %>: All PHP codes written within <% %> tags. Again, it is not recommended to use unless it is enabled in php.ini configuration file.

When PHP interpreter receives a .php file, it looks for the one of the starting PHP tag listed above to start interpreting PHP statements and continues until it finds its closing PHP tag.

Recommended Usage:
<?php ?> tags are most commonly used, hence, recommended.

PHP statement

  1. A statement is the smallest standalone element of a language. In the above example, line no. 9 is a statement.
  2. A PHP code may contain one or more statements.
  3. A  statement usually ends with a semicolon.

Displaying output in a browser

There are 2 ways to display output –

  1. echo: To display output in PHP, echo is used. In the above example in line no. 9 echo is used to display “Hello World”. Please note when you try to display string in PHP, you need to enclose the whole string with double quotes (as the above example shows) or single quotes, but not the combination of both (more on this later).
  2. print: print is also used to display output.

Though echo and print are identical in respect of displaying output, there is a subtle difference between those in performance. echo is a little bit faster than print.

PHP tags (<?php and ?>) placement

  1. Generally, PHP tags are embedded within the context of HTML (This is why PHP is called embedded language.). The above is a example of it.
  2. You can also write the PHP code in a separate file.

PHP statement termination

Each PHP statement is terminated by a semicolon (;). In the above example, at the end of line no. 9, semicolon is used to end the line. It indicates the end of that statement.

At the end of the last statement of a PHP code block, you may not use the semicolon because closing PHP tag (?>) implies semicolon and the code still is valid. In the above example, at the end of line 9, you may not use semicolon for the same reason.

Comments in PHP

  1. Comment is a smart way to document in plain English what code does.
  2. Adding comment is a good programming practice.
  3. PHP interpreter ignores to process it and remove it when it generates HTML output from PHP code.
  4. As PHP interpreter ignores comments, those are not visible in browser’s source code.

Why you should care comments?

  1. No matter how well you know your code today, it is natural that after 2 weeks or six months you’ll have no idea what the code does. Well commented script can help you remind everything.
  2. If you work in a team and forward one of your scripts that are not commented to another programmer, he’ll not understand your coding easily.

How to add comments?

There are three ways to add comments-

  1. One-line C++ style comment:
    This is the most commonly used way to add single line comment. It looks like two double forward slashes (//). After //, everything is ignored of that line to the end.
  2. Multi-line C style comment/Block comment:
    This type of comment starts with /* and ends with */. Anything between those are ignored by PHP interpreter.

    Programmers use this comment type to temporarily comment a portion of codes to find out bugs.
  3. One-line Unix shell-style comment:
    This type of comment starts with #(hash/pound) symbol. After #, everything is ignored of that line to the end.

    Many programmers use this to comment the introductory block of each script to notify purpose of the script, author name, author’s contact, creation date, modification date. Check the following example to see it in action.

    Comment Examples

    <! DOCTYPE html>
    
    # Author: Neil Fin
    # Script: Comments in PHP
    # Creation Date: 20th January, 2013
    
    
    <meta charset="”utf-8”" />
    
    
             <!--?php <br ?-->             /*
                    This is a
                    multi-line
                    comment
                 */
    
                 echo "Hello World"; // This is a single line comment
             ?>
          

PHP file extension

When you save a PHP file add .php as extension at the end of the file name. Example index.php. contact.php etc.

‹‹ PHP Installation : Previous Lesson Next Lesson: PHP Variables ››