PHP | Basic Syntax

PHP or Hypertext Preprocessor is a widely used open-source general purpose scripting language and can be embedded with HTML. PHP files are saved with “.php” extension. PHP scripts can be written anywhere in the document within PHP tags along with normal HTML.

PHP Tags or Escaping To PHP

The mechanism of separating a normal HTML from PHP code is called the mechanism of Escaping To PHP. There are various ways in which this can be done. Few methods are already set by default but in order to use few others like Short-open or ASP-style tags we need to change the configuration of php.ini file. These tags are also used for embedding PHP within HTML. There are 4 such tags available for this purpose:-

  1. Canonical PHP Tags:The script starts with <?php and ends with ?> . These tags are also called ‘Canonical PHP tags’. Every PHP command ends with a semi-colon (;). Let’s look at the hello world program in PHP:

    # Here echo command is used to print
    echo "Hello, world!";


    Hello, world!
  2. SGML or Short HTML Tags: These are the shortest option to initialize a PHP code. The script starts with <? and ends with ?>. This will only work by setting the short_open_tag setting in php.ini file to ‘on’.

    # Here echo command will only work if 
    # setting is done as said before
    echo "Hello, world!";

  3. HTML Script Tags: These are implemented using script tags. This syntax is removed in PHP 7.0.0. so its no more used.

    <script language="php">
    echo "hello world!";

  4. ASP Style Tags: To use this we need to set the configuration of php.ini file. These are used by Active Server Pages to describe code blocks. These starts with <% and ends with %>.

    # Can only be written if setting is turned on
    # to allow %
    echo "hello world";

Comments in PHP

A comment is something which is ignored and not read or executed by PHP engine or the language as part of a program and is written to make the code more readable and understandable. These are used to help other users and developers to describe the code and what it is trying to do. It can also be used in documenting a set of code or part of a program. You must have noticed this in above sample programs.
PHP supports two types of comment:

  • Single Line Comment: As the name suggests these are single line or short relevant explanations that one can add in there code. To add this, we need to begin the line with (//) or (#).

    // This is a single line comment
    // These cannot be extended to more lines
    echo "hello world!!!";
    # This is also a single line comment


    hello world!!!
  • Multi-line or Multiple line Comment: These are used to accomodate multiple lines with a single tag and can be extended to many lines as required by the user. To add this, we need to begin and end the line with (/*…*/)

    /* This is a multi line comment
        In PHP variables are written
        by adding a $ sign at the beginning.*/
    $geek = "hello world!";
    echo $geek;


    hello world!

Case Sensitivity in PHP

PHP is insensitive of whitespace. This includes all types of spaces that are invisible on the screen including tabs, spaces, and carriage return. Even one space is equal to any numbers of spaces or carriage return. This means that PHP will ignore all the spaces or tabs in a single row or carriage return in multiple rows. Unless a semi-colon is encountered, PHP treats multiple lines as a single command.

// PHP code illustrate the whitespace insensitivity
$var1         =     15;
$var2 =
$sum = $var1
// " " for new line
echo $sum, " ";
$sum1 = $var1 + $var2;
echo $sum1;



Here both of them show same results without any errors.

PHP is case-sensitive. All the keywords, functions and class names in PHP (while, if, echo etc) are NOT case-sensitive except variables. Only variables with different cases are treated differently. Let’s look at this example:

// Here we can see that all echo 
// statements are executed in the same manner
$variable = 25;
echo $variable;
ECHO $variable;
EcHo $variable;
// but this line will show RUNTIME ERROR as
// "Undefined Variable"



Blocks in PHP

In PHP, multiple statements can be executed simultaneously (under a single condition or loop) by using curly-braces ({}). This forms a block of statements which gets executed simultaneously.

$var = 50;
if ($var>0){
    echo ("Positive as ");
    echo ("greater than 0");


Positive as
greater than 0

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter