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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
    # Here echo command is used to print
    echo "Hello, world!";
    ?>

    chevron_right

    
    

    Output:



    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’.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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 %>.
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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 (#).
    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    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 (/*…*/)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    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.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code illustrate the whitespace insensitivity
$var1         =     15;
$var2 =
30;
$sum = $var1
+
$var2;
  
// "\n" for new line
echo $sum, "\n";
  
$sum1 = $var1 + $var2;
echo $sum1;
?>

chevron_right


Output:

45
45

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// 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"
echo $VARIABLE
?>

chevron_right


Output:

25
25
25

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$var = 50;
if ($var>0){
    echo ("Positive as \n");
    echo ("greater than 0");
}
?>

chevron_right


Output:

Positive as
greater than 0

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.