What is local variable in php with example?

PHP Variables Scope

In PHP, variables can be declared anywhere in the script.

The scope of a variable is the part of the script where the variable can be referenced/used.

PHP has three different variable scopes:

  • local
  • global
  • static

Global and Local Scope

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:

Example

Variable with global scope:

<?php
$x = 5; // global scope

function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();

echo "<p>Variable x outside function is: $x</p>";
?>

Try it Yourself »

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

Example

Variable with local scope:

<?php
function myTest() {
  $x = 5; // local scope
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();

// using x outside the function will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>

Try it Yourself »

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

PHP The global Keyword

The global keyword is used to access a global variable from within a function.

To do this, use the global keyword before the variables (inside the function):

Example

<?php
$x = 5;
$y = 10;

function myTest() {
  global $x, $y;
  $y = $x + $y;
}

myTest();
echo $y; // outputs 15
?>

Try it Yourself »

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

The example above can be rewritten like this:

Example

<?php
$x = 5;
$y = 10;

function myTest() {
  $GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}

myTest();
echo $y; // outputs 15
?>

Try it Yourself »

PHP The static Keyword

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:

Example

<?php
function myTest() {
  static $x = 0;
  echo $x;
  $x++;
}

myTest();
myTest();
myTest();
?>

Try it Yourself »

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

Note: The variable is still local to the function.

PHP Exercises



Definition of Local Variable in PHP

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.

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

<!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:

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 value.

<!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:

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.

<!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:

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.

Recommended Articles

This is a guide to Local Variable in PHP. Here we discuss definition, syntax, How does the local variable work in Php? examples with code implementation. You may also have a look at the following articles to learn more –

  1. Sort string PHP
  2. PHP Form Builder
  3. PHP Open File
  4. PHP list

What is local variables in PHP?

A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function −

What is local variable explain with example?

Declaration of Local Variable: In this case it is executed in the same manner as if it were part of a local variable declaration statement. For example: for(int i=0;i<=5;i++){……} In above example int i=0 is a local variable declaration. Its scope is only limited to the for loop.

What is variable in PHP explain with its scope with example?

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used. PHP has three different variable scopes: local. global.

What are local variables?

A local variable is a type of variable that can be used where the scope and extent of the variable is within the method or statement block in which it is declared. It is used as an iteration variable in the foreach statement, exception variable in the specific-catch clause and resource variable in the using statement.

Chủ đề