PHP Variables Local Global and Static Variables

Hello Friend how are you i hope you all are doing well. Today in this post we will learn Variables in PHP , Types of variable in PHP and Explanation of each type of variables. If you are learning PHP then this post will be very useful for you because in this post you will get the complete knowledge of variables in PHP.

Let's Start

👉Definition

It is a name of storage space which is used to store data
It's value may be changed.
It always contains last value stored to it.
Variable in PHP starts with dollar($) sign .

👉Example 1

<?php
$x=10;
$x=5;
$x=25;
echo "The value of x is ".$x;
?>
//output
The value of x is 25

👉Example 2

We can also use variable inside double quotes.
<?php
$str="Easy Softwares";
echo "I like $str";
?>
//output
I like Easy Softwares

👉Example 3

We can also concatenate variable with string using dot(.) operator.
<?php
$str="Easy Softwares";
echo "I like ".$str;
?>
//output
I like Easy Softwares

👉Rules to define a variable

  • It must starts with dollar sign.
  • There is no need of data type(int,float,char) to define a variables.
  • Variables can, but do not need, to be declared before assignment.
  • After dollar sign the first letter of a variable should be alphabet or underscore(_).
  • After dollar sign the first letter of a variable should not be digit.
  • After first character it may be combination of alphabets and digits.
  • Blank spaces are not allowed in variable name.

👉Types of Variable

There are three types of variable in PHP.
  • Local Variable
  • Global Variable
  • Static Variable

👉Local Variable

1.A variable declared inside the body of the method or constructor is called local variable.
2.Local variable can be used only inside that method/function in which it is declared.
3.A local variable can be a static variable.
<?php
//function definition
function myFunction()
{
$x=10;
$y=20;
echo "The value of x is $x and y is $y";
}
//function calling
myFunction();
?>
//output
The value of x is 10 and y is 20

👉Global Variable

1.A variable which is declared outside the body of the method or constructor is called global variable.
<?php
$x=5;
$y=6;
//function definition
function myFunction()
{
$x=10;
$y=20;
echo "<b>Inside function:</b> The value of x is $x and y is $y <br>";
}
//function calling
myFunction();
echo "<b>Outside function:</b>The value of x is $x and y is $y ";
?>
//output
Inside function: The value of x is 10 and y is 20 
Outside function:The value of x is 5 and y is 6

👉Static Variable

1.A variable which is declared with static keyword is called static variable.
2.Static variable is stored in the static memory.
3.Static variables are created when the program starts and destroyed when the program stops.

<?php
//function definition
function myFunction()
{
$x=10;
echo "$x<br>";
$x++;
}
//function calling
myFunction();
myFunction();
myFunction();
?>
//output
10
10
10

<?php
//function definition
function myFunction()
{
static $x=10;
echo "$x<br>";
$x++;
}
//function calling
myFunction();
myFunction();
myFunction();
?>
//output
10
11
12

👉Use of Global variable as a local variable method 1

<?php
$x=5;
$y=6;
//function definition
function myFunction()
{
global $x,$y;
echo "<b>Inside function:</b> The value of x is $x and y is $y <br>";
}
//function calling
myFunction();
echo "<b>Outside function:</b>The value of x is $x and y is $y ";
?>
//output
Inside function: The value of x is 5 and y is 6 
Outside function:The value of x is 5 and y is 6

👉Use of Global variable as a local variable method 2

<?php
$x=5;
$y=6;
//function definition
function myFunction()
{
$sum=$GLOBALS['x']+$GLOBALS['y'];
echo "<b>Inside function:</b> Sum=$sum";
}
//function calling
myFunction();
$sum=$x+$y;
echo "<b>Outside function:</b> Sum=$sum";
?>
//output
Inside function: Sum=11
Outside function: Sum=11

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thanks.

Post a Comment

0 Comments