PHP 變量的作用域
變量的作用域被定義為程序中可以訪問到它的范圍。愛掏網 - it200.com換句話說,”變量的作用域是在其定義和可以訪問的程序部分內”。愛掏網 - it200.com
PHP有三種類型的變量作用域:
- 局部變量
- 全局變量
- 靜態(tài)變量
在函數(shù)內部聲明的變量稱為該函數(shù)的局部變量。愛掏網 - it200.com這些局部變量的作用域僅限于它們聲明的特定函數(shù)內部。愛掏網 - it200.com這意味著這些變量無法在函數(shù)外部訪問,因為它們具有局部作用域。愛掏網 - it200.com
與函數(shù)內部聲明的變量不同,函數(shù)外部以相同名稱聲明的變量是完全不同的。愛掏網 - it200.com讓我們通過一個示例來理解局部變量:
文件:local_variable1.php
<?php
function local_var()
{
num = 45; //local variable
echo "Local variable declared inside the function is: ".num;
}
local_var();
?>
輸出:
Local variable declared inside the function is: 45
文件:local_variable2.php
<?php
function mytest()
{
lang = "PHP";
echo "Web development language: " .lang;
}
mytest();
//using lang (local variable) outside the function will generate an error
echolang;
?>
輸出:
Web development language: PHP
Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28
全局變量
全局變量是在函數(shù)外部聲明的變量。愛掏網 - it200.com這些變量可以在程序的任何地方訪問。愛掏網 - it200.com要在函數(shù)內部訪問全局變量,請在變量前使用GLOBAL關鍵字。愛掏網 - it200.com然而,這些變量也可以直接在函數(shù)外部訪問或使用,無需任何關鍵字。愛掏網 - it200.com因此,無需使用任何關鍵字來訪問函數(shù)外部的全局變量。愛掏網 - it200.com
讓我們通過一個示例來理解全局變量:
示例
文件:global_variable1.php
<?php
name = "Sanaya Sharma"; //Global Variable
function global_var()
{
globalname;
echo "Variable inside the function: ". name;
echo "</br>";
}
global_var();
echo "Variable outside the function: ".name;
?>
輸出:
Variable inside the function: Sanaya Sharma
Variable outside the function: Sanaya Sharma
注意:如果不使用global關鍵字,在函數(shù)內部嘗試訪問全局變量時會產生一個錯誤,提示變量未定義。愛掏網 - it200.com
示例
文件:global_variable2.php
<?php
name = "Sanaya Sharma"; //global variable
function global_var()
{
echo "Variable inside the function: ".name;
echo "</br>";
}
global_var();
?>
輸出:
Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6
Variable inside the function:
使用$GLOBALS
而不是global
在函數(shù)內部使用全局變量的另一種方式是預定義的$GLOBALS數(shù)組。愛掏網 - it200.com
示例:
文件:global_variable3.php
<?php
num1 = 5; //global variablenum2 = 13; //global variable
function global_var()
{
sum =GLOBALS['num1'] + GLOBALS['num2'];
echo "Sum of global variables is: " .sum;
}
global_var();
?>
輸出:
Sum of global variables is: 18
如果兩個變量local和global有相同的名稱,那么在函數(shù)內,局部變量比全局變量具有更高的優(yōu)先級。愛掏網 - it200.com
示例:
文件:global_variable2.php
<?php
x = 5;
function mytest()
{x = 7;
echo "value of x: " .$x;
}
mytest();
?>
輸出:
Value of x: 7
注意:本地變量優(yōu)先于全局變量。愛掏網 - it200.com
靜態(tài)變量
PHP有一個特性,即在變量執(zhí)行完畢并釋放內存后會刪除變量。愛掏網 - it200.com有時候,我們需要在函數(shù)執(zhí)行完畢后仍然保留變量。愛掏網 - it200.com因此,變量作用域的另一個重要特性是靜態(tài)變量。愛掏網 - it200.com我們在變量前面使用static關鍵字定義一個變量,這個變量被稱為 靜態(tài)變量 。愛掏網 - it200.com
靜態(tài)變量只存在于局部函數(shù)中,并且在程序執(zhí)行離開作用域后不釋放其內存。愛掏網 - it200.com通過以下示例來理解:
示例
文件:static_variable.php
<?php
function static_var()
{
static num1 = 3; //static variablenum2 = 6; //Non-static variable
//increment in non-static variable
num1++;
//increment in static variablenum2++;
echo "Static: " .num1 ."</br>";
echo "Non-static: " .num2 ."</br>";
}
//first function call
static_var();
//second function call
static_var();
?>
輸出:
Static: 4
Non-static: 7
Static: 5
Non-static: 7
你必須注意,在每次函數(shù)調用后,num1會定期遞增,而num2不會。愛掏網 - it200.com這是因為$num1不是一個靜態(tài)變量,所以它在每次函數(shù)調用后釋放了內存。愛掏網 - it200.com