Table of Contents
Syntax
How does the local variable work in Php?
Examples
Example #3
Conclusion
Home Backend Development PHP Tutorial Local Variable in PHP

Local Variable in PHP

Aug 29, 2024 pm 12:35 PM
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.

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
?>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

See all articles