Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP - A brief Overview - Part -1

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 347
    Comment on it

    This posts will provide you a brief overview of PHP which is the most popular web development language. It is targeted to the audience having basic knowledge of programming. The readers searching for more in-depth knowledge will be disappointed.

    This series is divided into several posts each of which will cover different topics of PHP from variable declaration Error Handling to performing database operations.

    This first post will provide an overview of PHP variables. What they are? How they can be used? How to output the values variable contains? What is the scope of a variable?, etc.

     

    Variables are the named storage location. PHP have some rules for declaring and using these variables.

    1. Variables must always be preceded by “$” sign.

    2. Variables must start with a letter or an underscore.

    3. Variables can only contain alpha-numeric characters and underscore.

    4. Variables can be declared anywhere in the script.

    5. Variables in PHP are case sensitive that means $myVar and $myvar are two different variables and can have different values simultaneously.

    6. Variables do not need any command to for their declaration.

    7. Variables get created at the moment whenever they are assigned some value for the first time.


    Examples :

    <?php
    $text = Hello World!;
    $x = 5;
    $y = 10.5;
    ?>

    Note : PHP is a loosely typed language which means there is no need to tell the data-type of a variable.

    Outputting variables :

    “echo” statement outputs the value of variables.

    Example :

    <?php
    $lang = PHP;
    echo I love $lang!;
    ?>

     

    Scope of a variable :

    The scope of a variable is that part of the script where the variable can be referenced/used.

    A PHP variable can have any one of the three scopes namely, local global and static.

    Global Variables :

    These variable are declared outside a function and can only be accessed outside a function.

    Example :
     

    <?php
    $x = 5; // global scope
    
    function myTest()
    {
    // Using x inside this function will generate an error.
    echo <p>Variable x inside function is : $x</p>;
    }
    
    myTest();
    
    echo <p>Variable x outside function is : $x</p>;
    ?>
    
    

    Local Variables :

    Local variable are always declared within a function. They can only be accessed within that functions.

    Example :
     

    <?php
    function myTest()
    {
    $X = 5; //Local scope.
    echo <p>Variable x inside function is : $x</p>;
    }
    
    myTest();
    
    // Using x outside this function will generate an error.
    echo <p>Variable x outside function is : $x</p>;
    ?>
    
    

     

    The “global” keyword :

    This keyword is used to access a global variable from within a function.

    Example :
     

    <?php
    $x = 5;
    $y = 10;
    
    function myTest()
    {
    global $x, $y;
    $y = $x + $y;
    }
    
    myTest();
    
    echo $y; // Outputs 15
    ?>
    
    

    All global variables are also stored in an array called $GLOBAL[index].

    The index holds the name of the variable.

    $GLOBAL[index] is also accessible from within functions and can be used to update global variables directly.

    The “static” keyword :

    The “static” keyword is used to mark a variable as static which means the variable will persists in memory along with its data even at the end of its scope.

    Example :
     

    <?php
    function myTest()
    {
    static $x = 0;
    echo $x;
    $x++;
    }
    
    myTest();
    myTest();
    myTest();
    ?>
    
    

    Outputting Data :

    PHP offers two statements to output data viz. “echo” and “print”

    Example :

    Echo :

    <?php
    echo <h2>PHP is Fun!</h2>;
    ?>

     

    Next part will be about datatypes and string functions.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: