Varibles In PHP With Example & Use

Variable Declaration In PHP & Use Of Variable In PHP
\\ If you have any query or questions on my post, then please comment below //

Variable: Before going to work on variables First, you should know very well, What is variable. Please click here to know, what is variable
 Now above link you learn variable in c programming, but the definition of the variable is same for all the programming languages, Only the use of variable will differ, according to the programming language.
Now you see in above link use of the variable, in c programming. Here you see for every variable you declare his data type. Like: int a; and for reading and printing the value you used data type symbol. For Ex: for taking value in c: scanf ("%d",&a); for printing value of c: printf ("%d",a);

But in PHP, you don't need to do these things. Here no need to declare data type and need to write the data type symbol for taking and printing value. You just have to use "$" sign for declaring any value to variable. For example: Use a as variable in PHP: $a; I write. If I want to give the value = 10 in a variable, Then I will write. $a=10;
And for printing value in PHP, I Use echo;
For Ex:              
$a=10;
echo $a;   

PHP Variable Example:
Program: Write a program to take 2 values, First "10" and second "Technical Programming". And print these two values.
<?php
$a=10;                                    //
$b= Technical Programming;   //     Input Given
echo $a;                                 //
echo $b;                                 // Output Given
?> 

Types Of Variable In PHP:
In PHP, There are 3 types of variables: 
                                                             1. Local Variable.
                                                             2. Global Variable.  
                                                             3. Static Variable.

Local Variable In PHP: 
A variable that is used or declared only inside a function, that can't be accessed by outside the function. It is limited under a function. This type of variable is called local variables.
Example Of Local Variable In PHP:
 <?php
function add()

    $a=20;           //  Local Variable
    $b=10;           //  Local Variable
    $c = a+b;                             
    echo $c;                             
?>
Run the above program and see the output, and comment below the output. 

Global Variable In PHP:
A variable that is used or declared outside a function, that can't be accessed by inside the function. It is limited only to his own function, In other functions you can use.     
Example Of Global Variable In PHP:
<?php
$a=10;                     // global variable or scope
function Test(

{
  $b=10;                  // local variable or scope
  echo "<p>Test variables inside the function:</p>";
  echo "Variable x is: $a";
  echo "<br>";
  echo "Variable y is: $b";
}

TechnicalTest();
echo "<p>Test variables outside the function:</p>";
echo "Variable a is: $a";
echo "<br>";
echo "Variable b is: $b";
?> 

Run the above program and see the output, and comment below the output.   

Static Variables In PHP:
As per the name you can say static means fixed. When a function or task is completed his single or multiple task, all of its variables are deleted or changed. So, sometimes we want a variable not to be deleted or changed. We need it for a further task. So that kind of variable is known as static variables.  
Example Of Static variables In PHP:
<?php
function Test() {
  static $a=0;
  echo $a;
  $a++;
}
Test();
Test(); 
Test();Test();
Test();
?> 

Run the above program and see the output, and comment below the output.   

In this post you learn:
1. What is variable.
2. Difference between php variable and other programming variables.
3. How to use variables in php with example.
4. Types of variables in php.
5.  What is a local variable with examples.
6.  What is a global variable with examples.
7. What is a static variable with examples. 
If you have any doubt in above 7 points then please comment below. 


 Enter Your Email Id To Get Our Programming, Web Development, Video Tutorial, Job Updates In The Mail:

Labels: