Home >Backend Development >PHP Tutorial >Local Variable in PHP

Local Variable in PHP

WBOY
WBOYOriginal
2024-08-29 12:35:09346browse

Local variables are those variables that are declared inside the function of a Php program and have their scope inside that function only. Local variables have no scope outside the function (variable cannot be referenced outside the function), so cannot be used outside its scope in the program. If any other variable with the same name is used in a program outside a function(a global variable), it is considered differently and has its own identity, and considered as a completely different variable. Local variables follow the same characteristics of a normal variable, i.e. starting with the ‘$’ sign and the variable name starting with (a-z) or underscore ( _ ) sign.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

If we talk about the syntax, there is no such syntax of using the local variable in an oho program. The program needs to define the variable inside a function and use it there only.

<?php
//here var1 is a global variable
$var1= 900;
//php function
function xyz()
{
//here var1 is a local variable
//so can be used inside this function only
$var1 ='abc';
// some php function code
}
locVar();
// php code
?>

How does the local variable work in Php?

There are basically 3 broad categories of variables in Php, i.e. Local Variable, Global Variable, and Static Variables. All the variables have the difference in the scope and the way they are defined in the program. Elaborating the local variables in this article, below given are some of the important points which the programmer needs to understand in order to have a clear vision of a local variable in Php:

Local variables are declared and used inside the function only. Local variables in Php have the Local scope (cannot be used outside the function). If a global variable exists with the same name as the local variable in the program, they have nothing to do with each other. They both are completely different from each other.

When the local variable is called inside the function, its value will get printed on the console. Local variables, if printed or used in any way outside the function in a php program, give an error to the user. Like the normal variable in Php, the local variable also starts with the ‘$’ sign.

Examples

It is important to perform and try things programmatically in order to have a better understanding. Below given are some of the examples of the Php program showing the usage of the local variables:

Example #1

Program to print the value of local variable outside the function

Code:

<!DOCTYPE html>
<html>
<body>
<?php
//php function
function myLocal() {
// local variable 'name' having the local scope
$name = 'Rajesh';
echo "<p>Hello the value of local variable inside the function is : $name </p>";
}
//calling the function
myLocal();
// printing the value of local variable outside the function, gives an error
echo "<p>Value of local variable outside the function is : $name </p>";
?>
</body>
</html>

Output:

Local Variable in PHP

Explanation:

In the above example, ‘myLocal’ is the function in Php and ‘name’ is the local variable of the function ‘myLocal’ having the value ‘Rajesh’. Function myLocal is called. When the value of the local variable ‘name’ is printed on the console inside the function, ‘Rajesh’ is printed and on printing the value of that variable outside the function, nothing is displayed as the variable ‘name’ has the local scope.

Example #2

Program having the value of both local and Global variables with the same name and different values.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
// global variable
$name = 'Ankita';
function myLocal() {
$name = 'Rajesh'; // local variable having the local scope
echo "<p>Hello the value of local variable inside the function is : $name </p>";
}
//calling the function
myLocal();
// printing the value of variable outside the function, will consider the global function
echo "<p>Value of variable outside the function is : $name </p>";
?>
</body>
</html>

Output:

Local Variable in PHP

Explanation:

In the above example, myLocal() is the name of the function having the local variable ‘name’ with the value ‘Rajesh’. There is a variable ‘name’ with the value ‘Ankita’ defined at the starting of the code outside the function ‘myLocal’. When the value of the variable ‘name’ is printed on the console inside the function, ‘Rajesh’ is printed whereas when it is printed outside the function, ‘Ankita’ is printed as both the variables ‘name’, although having the same name but are completely different from each other. They have nothing to do with each other.

Example #3

Program having the 2 functions with the same name of variables in both the functions.

Code:

<!DOCTYPE html>
<html>
<body>
<?php
//function addition with the 2 local variables 'value1' and 'value2'
function addition()
{
$value1 =95;
$value2 =20;
$addition =$value1 + $value2;
echo "<p> Result of the above addition : $addition </p>";
}
//function subtraction with the 2 local variables 'value1' and 'value2'
function subtraction()
{
$value1 =99;
$value2 =9;
$subtraction =$value1 - $value2;
echo "<p> Result of the above subtraction : $subtraction </p>";
}
//calling the above 2 functions
addition();
subtraction();
// printing the values of the local variables outside the function
echo "<p> Result of the above addition outside function : $addition </p>";
echo "<p> Result of the above subtraction outside function : $subtraction </p>";
?>
</body>
</html>

Output:

Local Variable in PHP

Explanation:

In the above example, 2 functions are used,i.e. addition and subtraction respectively. Both the functions have the local variables ‘value1’ and ‘value2’. Both the variables have their scope inside their own functions only. Addition and Subtraction are performed inside the functions and the result is stored in their local variables ‘addition’ and ‘subtraction’ respectively. When the values of these local variables are printed inside their respective functions, the results are displayed on the console. When the values of these variables are printed outside the functions, nothing is displayed to the user.

Conclusion

Above description completely explains what are local variables in Php and how they are used in a Php program in their local scope only. Before proceeding for the advanced concepts, it is very important for a programmer to understand the basic thing clearly and use them in a program for clear and in-depth knowledge of the concepts.

The above is the detailed content of Local Variable in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP Global VariableNext article:PHP Global Variable