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 TestsStart 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:
<?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 "; } //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>"; ?>
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 values.
Code:
<?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 "; } //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>"; ?>
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.
Code:
<?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 "; } //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>"; ?>
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.
The above is the detailed content of Local Variable in PHP. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool