Php 简明教程
PHP - Local Variables
作用域可以定义为变量在其中声明的程序中可用性的范围。PHP 变量可以是四种作用域类型之一−
Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −
-
Local Variables
-
Global Variables
-
Static Variables
-
Function Parameters
Local Variables
在函数中声明的变量被视为局部变量;也就是说,它只能在该函数中引用。该函数之外的任何赋值都将被视为与函数中包含的变量完全不同的变量 -
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 −
<?php
$x = 4;
function assignx () {
$x = 0;
print "\$x inside function is $x. \n";
}
assignx();
print "\$x outside of function is $x. \n";
?>
这会产生以下结果 −
This will produce the following result −
$x inside function is 0.
$x outside of function is 4.