PHP Syntax and Variables

Discussion in 'PHP' started by MinalS, Oct 15, 2015.

  1. PHP tags



    The PHP tags are used by the user to divide the PHP code from the page elements. The content inside the tags is used and remaining is ignored by the compiler.

    The various PHP tags useful for coding are mentioned below:

    1. Short Open tags - The short open tags are useful as the smallest tags options. The tag is defined as <? …?>. The tag can be detected using one of the following two options.
      • In the php.ini file, add the short_open_tag is set. For parsing the XML, the option is disabled.
      • The –enable-short-tags configuration option is selected in PHP
      Canonical PHP tags - The PHP tag which is used in almost every application is the canonical tag. It is represented as <?php…?>.
      HTML script tags - The tag is defined as <script language = "PHP">….</script>
      ASP style tags - The tags that are used by the Active Server Pages are known as ASP – style tags. The tag is defined as <%...%>.

    Comments in PHP



    The comments is a part of the program that is useful for the users. The compiler ignores the comments defined in the code. Detailed information about the code is provided in the comments. In PHP, two types of comments can be defined.

    1. Single – line comments

    The single line comments are used when small detail about the code is to be described. The symbols like # and // are used for defining the single line comments.

    Example:
    Code:
    #Comment used for describing code
    //Addition of two numbers
    //Print the result
    
    2. Multi – line comments

    When more than one line comment is to be defined, the multi line comments is used. The comment is represented by the character pair /* and ends with pair */.

    The nesting of multi line comments is not possible. If user performs nesting, the comment encounters the first instance of */ and closes the comments.

    Example:
    Code:
    /* Comments defined multi
    Line code */
    

    PHP is Case Sensitive



    PHP is a case sensitive language. The programmer must add appropriate captions for text to be displayed.

    Example:
    Code:
    <html>
        <body>
        <?php 
        $age = 10;
        print ( "The age is $age<br>")
        print ( "The age is $Age<br>")
        </body>
    </html>
    
    The output generated when the code is compiled and executed.

    The age is 10
    The age is

    In PHP, any expression is terminated by a semicolon. The statements added in the PHP tags is valid.

    Code:
    $message = "Learn PHP!!";
    

    Executing PHP from command prompt



    1. Add the code in the demo.php file.
    Code:
    <? php
        print "Welcome User";
    ?>
    
    2. In the command prompt, add the following command
    Code:
     $ php demo.php 
    3. The output generated when the code is compiled and executed
    Welcome User

    Overview of variables



    The variables in PHP are used for saving the value of the program. These values can be used later whenever needed by the programmer. The latest value assigned to the variable is its value. All the variables are preceded by a $ sign. User does not need to declare the variable before the assignment. The = operator is used for assigning the values to the variable.

    The variables contain default values when they are not assigned. The intrinsic type is not supported by PHP variables. The type conversion of variables can be easily done in PHP.

    Different PHP data types



    The data types define the type of value to be stored in the variables. There are eight data types provided in PHP. They are explained below:

    1. Integer

    It is the basic data type in PHP. They represent whole numbers. The numbers can be positive or negative and do not contain decimal point. User can assign the integer values to the variable and can be used in the expressions.

    Code:
    $int_num1 = 50;
    $int_num2 = -70;
    
    The integers can be used in three different formats with respect to their bases as decimal ( base10 ), octal ( base 8 ) and hexadecimal ( base 16). The octal values are represented with 0 as leading value. The hexadecimal number has 0x as the leading value. The decimal is the default type.

    The smallest integer value is - 2,147,483,647 and largest integer value is 2,147,483,647.

    2. Booleans

    The Boolean variable can contain only two values as true or false. They are represented as TRUE and FALSE constants.

    Consider the following snippet to demonstrate the use of Booleans.

    Code:
    if ( TRUE )
        print("If the condition is satisfied then the value is printed");
    else
    print ("Value is not printed")
    
    Some of the rules that a user must remember while using the Boolean value are mentioned below:
    1. The value of NULL type are considered as FALSE
    2. If a string is defined, it is false if it is empty, else considered as true
    3. Double data type cannot be used in Booleans
    4. If a number is defined, it is false if is equal to zero, else true
    5. The array must contain a value to be true, else defined as false
    The truth value of the Boolean data is defined as follows:
    Code:
    $array1 [10 ] = "Defining an element in an array";
    $num1 = 10+20;
    $null1 = NULL;
    $string1 = " ";
    
    3. Doubles

    The floating point numbers are represented as doubles. They are defined with minimum number of decimal points.

    The following code represents the doubles in PHP.
    Code:
    <?php
        $ num1 = 4.20;
        $ num2 = 3.40;
        $ num3 = $num1 + $num2;
        print ( " $num1 + $num2 = $ num3 <br>");
    ?>
    
    The following output is generated when the code is compiled and executed
    4.20 + 3.40 = 7.6

    4. NULL

    The data type contains a single value as NULL. The value can be assigned to the variable as $ var1 = NULL. The constant is not case sensitive. User can defined it as null also.

    5. Strings

    A group of characters are defined inside the string variable. Single or double quotation marks are used for defining them. In single quotes, the string are considered literally. In double quotes, the variables are replaced with the corresponding values.

    Example to demonstrate the String functionality.
    Code:
    <?php
        $var = "class";
        $literal = ‘I am in 5 $var ‘;
        print ( $literal );
        print "<br>";
        $literal = "I am in 5 $var";
        print ( $literal);
    ?>
    
    The output generated when the code is compiled and executed.
    I am in 5 $var
    I am in 5 class

    Scope of variables



    The variables in PHP are available to a specific limit in the coding. There are four types of variable scope defined as mentioned below:

    1. Local variables

    The local variables are the one defined inside the function. The variable declared outside the function are corresponding to another function.
    Code:
    <?php
        $a = 10;
        function add()
        {
            $a = 1;
            print "Value of $a is: <br>";
        }
        add();
        print "Value of $a here is:<br>";
    ?>
    
    Output for the above code is as shown below:
    Value of $a is 1
    Value of $a here is 10

    2. Global variables

    The global variables are accessed anywhere in the program. The variable is explicitly declared to be global in a function. The GLOBAL keyword is used before the variable name.
    Code:
    <?php
        $var1 = 20;
        function display()
        {
            GLOBAL $var1;
            $var1 ++;
            print " Variable is: $var1;
        }
        display();
    ?>
    
    The result generated when the code is executed is:
    Variable is: 20

    3. Static variables

    The static variables maintain its value even when the user exits the function. The STATIC keyword is used to define the static variable.
    Code:
    <?php
        function increase()
        {
            STATIC $counter = 0;
            $counter ++;
            print $counter;
            print "<br/>";
        }
        increase();
        increase();
    ?>
    
    The output generated when the code is executed is:
    1
    2

    4. Function parameters

    They are defined following the name of the function and in the parentheses. The declaration is similar to the variable.
    Code:
    <?php
        function add ( $var )
        {
            $var = $var + 20;
            return $var;
        }
        $var = add ( 5 );
        print "Value is: $var \n";
    ?>
    
    When the code is executed, the following output is generated
    Value is: 25
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice