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?
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..),
PHP OUTPUT VARIABLES
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

    1. Global
    2. Local
    3. Static
Global Variable
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. ?>
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
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
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!.