Learn About PHP Variables

Introduction 

 
In my previous blog, I talked about a basic introduction to PHP.
 
In this blog, we will discuss "PHP VARIABLES".
 

PHP Variables

 
What is Variable?
  • Variables are used to store the value and access the stored value in the program.
In PHP, variables begin with the "$" symbol, followed by the Variable name.
 
Example:
  1. <?php  
  2.   $a = 2;  
  3. ?>  
Rules for assigning a Variables:
 
Basically, a Variable can have a short name (like i,e., x,y,etc..) or larger names (like i,e., first,age_group,etc..),
  • A Variable name must start with underscore characters or with a letter.
  • A Variable name cannot begin with numbers.
  • A variable name can contain only alpha-numeric characters and underscores ( A-Z, 0-9 and _ ).
  • Variable names are case-senstive($NAME, $name), these names are two different variable names.
PHP OUTPUT VARIABLES
  • The "echo" statement is often used to show the output to the screen.
Example 1
  1. <?php  
  2.    $m = "BEST WISHES!";  
  3.    echo"$m";  
  4. ?>  
Example 2
  1. <?php  
  2.     $a = 2;  
  3.     $b = 1;  
  4.     echo $a + $b ;  
  5.  ?>  

PHP VARIABLES SCOPE

  • Variables are declared anywhere in the program script.
  • A PHP consist of three types of variable scopes:
    1. Global
    2. Local
    3. Static
Global Variable
  • A variable declared outside of the function is known as a global variable.
  • It can be accessed outside of the function and anywhere in the program.
Example 1
  1. <?php  
  2.    $x = 2; // global variable  
  3.    function test()  
  4.    {  
  5.       echo"<p>WELCOME: $x</p>";  
  6.    }  
  7.    test();  
  8.    echo"<p>WELCOME : $x</p>"  
  9. ?>  
  • The global variables are accessed from inside of the function with the help of the keyword "global".
  • Use the "global" keyword before the declared function variable (inside).
Example 2
  1. <?php  
  2.    $a = 2;  
  3.    $b = 2;  
  4.    function test()  
  5.    {  
  6.       global $a$b// global keyword is used  
  7.       $b = $a * $b;  
  8.    }  
  9.    test();  
  10.    echo $b ;  
  11. ?>  
Local Variable
  • A variable declared inside of the function is known as local variable.
  • It can be accessed only inside that function.
  • The local variables can have the same names with different functions because they can be recognized only by their declared function names.
Example:
  1. <?php  
  2.    function test()  
  3.    {  
  4.       $x = 2; // local variable  
  5.       echo"<p>WELCOME: $x</p>";  
  6.    }  
  7.    test();  
  8.    echo"<p>WELCOME FUNCTION IS : $x</p>";  
  9. ?>  
Static Variable
  • Generally,when the function gets executed,all the variables in the function are deleted.
  • Sometimes, we want some of the local variables for further references.
  • For that purpose, use the "static" keyword before the variable in the function declaration.
Example
  1. <?php  
  2.    function test2()  
  3.    {  
  4.       static $a = 1; // static variable  
  5.       echo"$a";  
  6.    }  
  7.    test2();  
  8. ?>  
These are all the PHP Variables.
 
Here, I have attached the example codings for the above-discussed topics. Kindly refer to it.
 
Thank you!.