Variables are used to store information which can be referenced and manipulated within a computer program. These variables also play a vital role in labeling data with a descriptive name, which makes it easier for the users to understand the program clearly and impressively. Some programmers also consider the variables as containers which can hold information.
In every program, the sole purpose of a variable is to label the data and store it, somewhere in the memory. This data can be used throughout the program, anytime sooner or later.
When it comes to PHP, declaring and storing variables are different. The variables in PHP hold the value or information like string, integar etc. In PHP, a variable is declared by using the “$” sign. It should be followed by the name of the variable.
While declaring variables in PHP, you might have to follow few basic rules, including the following:
For example: $name=”hello world”;
Here $name is a variable and “hello world” value.
Example of how to store string, integer, and float values in PHP variable:
$string=”hello I am a string variable”; //this is a php string value
$integer=600; // this is a php integer value
$float=50.8; // this is a php float value
echo “this is a string value: $string<br>”;
echo “this is a integer value: $integer<br>”;
echo “this is a float value: $float<br>”;
this is a string value :hello I am a string variable
this is a integer value: 600
this is a float value: 50.8
Addition of two number-
Echo “sum of two number is: $result”;
Sum of two number is: 500
Scope of a variable:
The scope of a variable can be defined as its extent in program. It can be accessed in the program. Easily explained, it is the portion of a program within which it is clearly visible and accessible. PHP has three variable scopes:
These are the variables which are declared within a function. It is so named as it has scope only within that particular function. It is not able to access outside the particular function. Any declaration of a variable that you use outside the function with the similar name, as the one used within the function, becomes a completely different one.
$num = 60;
//This $num is local to this function
//the variable $num outside this function
//is a completely different variable
$num = 50;
echo “local num = $num \n”;
//$num outside function local_var() is a
// completely different variable than that of
// inside local_var()
echo “Variable num outside local_var() is $num \n”;
Local num = 50
Variable num outside local_var() is 60
The variables that are declared outside a particular function are known as global variables. One can access directly outside that particular function. In order to access the variable within a function, one need to use the word “global”, in front of the variable to refer to the particular global variable.
$num = 20;
// functionto demonstrate use of global variable
//we have to use global keyword before
// the variable $num to access within
// the function
echo “variable num inside function : $num \n”;
echo “Variable num outside function : $num \n”;
Variable num inside function : 20
Variable num outside function : 20
It is the characteristic of PHP to delete a variable. Once the variable completes its execution and the entire memory is freed, it is to be deleted. But sometimes, the variables need to be stored even after the function is executed. To conduct this, PHP developers use static keyword and the variables then are known as the static variables.
// function to demonstrate static variables
// static variable
static $num = 5;
$sum = 2;
echo $num, “\n”;
echo $sum, “\n”;
// first function call
// second function call
Learn more about PHP variables and broaden your knowledge under the expert guidance at Webskitters Academy! We provide complete training to transform you from a novice to an expert in the market.