PHP variable definition and details of use global , static , and array inside or outside of Scope

PHP variable

PHP variable

To store a volatile data in a page and to use it, variable is the most useful portion of any programming language. In this post we are going to see how to use php variable with its definition it contain php global variable, php array and php static variable with simple php variable and how the variable behaves inside or outside of Scope.

Generally in PHP a variable is declared by placing $ before the name of the variable. There is no separation of int or float etc, in case of PHP only place a dollar ( $ ) before the variable name is done.

An example of php variable is given below:

<?php
$name = “Name Goes Here”;
echo $name;
?>

 

How to write a php variable and whats the structure to follow?

As already told that to write a php variable name place $ (dollar) before the variable name without any space and after the variable name place a equal ( = ) and then assign the value on it.

Remember the restriction to write a php variable.

* do not give any space between the $ and variable name.
* do not use any special character except _ (underscore).
* should not use a _ before the variable name as its mainly used in php predefined key variable.

How php variable behaves inside or outside of Scope?

In a simple word if a variable is declared outside of scope will nor work inside a scope and similarly if a variable is declared inside of a scope will not work outside of the scope if its not a global or static variable.

Explanation with example:

If a variable is declared first and then a function is declared, in that case if user want to use the variable it will throw error. but after the end of the function the variable can be used again.

<?php
$name = “My Name”;

function callName(){
echo $name; //will generate an error
}

callName();

echo $name; //will Print ‘My name’
?>

 

Similarly if a function is declared first and inside the function a variable is declared, the variable will work inside the function but outside of the function, that is after ends of the function if user want to use the variable will returns error.

<?php
function callName(){
$name = “My Name”;
echo $name; //will Print ‘My name’
}

callName();

echo $name; //will generate an error
?>

 

How to declare and use php global variable?

As i already told that a global variable works inside and outside of scope after declaring the variable. Now to declare a global variable , first write GLOBAL and then giving space the variable name with prefix as $ ( dollar ). also a global variably can be declared without the sign $ by passing the variable name inside the array of GLOBAL.

*After declaring the php global variable there can be assign any value whenever needed unlike php static variable.

an example of php global variable is given below:

<?php
$a = 15;
$b = 20;

function myGlobalAddition() {
global $a;
GLOBAL['b'];
$b = $a + $b;
}

myGlobalAddition();
echo $b; //the outputs will be 35
?>

 

How to define and use PHP Static Variable?

A normal variable can be used inside scope only, after the scope the variable and its value is garbeg collected. Sometime we have to use the value of the variable outside of the scope. so that time if a variable is declared as static then its value will not be garbeg collected.

an example of php static variable is given below.

<?php
function myStaticIncrement() {
static $a;
$a = 1;
echo $a;
$a++;
}

myStaticIncrement();
myStaticIncrement();
myStaticIncrement();
//the outputs will be
// 1
// 2
// 3
?>

 

How to declare and use a PHP Array?

To declare a php array user have to write a variable name using $ ( dollar ) as a prefix and then after equal have to pass the value with key of the array rows inside array function. There is two type of array signed and unsigned, also according to dimension php array is two type and they are single dimensional and multi dimensional array. We will read about php array in another post now here I have shown how to declare a php array.

An example of php array is given below:

<?php
$unsigned_arr = array(‘red’, ‘blue’, ‘green’); // its a unsigned array

$signed_arr = array(‘clr1’ => ‘red’, ‘clr2’ => ‘blue’, ‘clr3’ => ‘green’);//Its a signed array

$multy_dimensional_array = array(
‘arr_ky1’=>array(‘red’, ‘blue’, ‘green’),
‘arr_ky1’=>array(‘clr1’=>’red’, ‘clr2’=>’blue’, ‘clr3’=>’green’)
); // The array is a multi dimensional array

?>

 

In this post we have gone through php variable definition and details of use , php global variable , php array , php static variable and how does php variable behave inside or outside of Scope , I hope that you got a clear idea on the topic.

Thanks for reading.

Share The Post On -

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.