PHP 变量的作用域

PHP 变量的作用域

变量的作用域被定义为程序中可以访问到它的范围。换句话说,”变量的作用域是在其定义和可以访问的程序部分内”。

PHP有三种类型的变量作用域:

  1. 局部变量
  2. 全局变量
  3. 静态变量

局部变量

在函数内部声明的变量称为该函数的局部变量。这些局部变量的作用域仅限于它们声明的特定函数内部。这意味着这些变量无法在函数外部访问,因为它们具有局部作用域。

与函数内部声明的变量不同,函数外部以相同名称声明的变量是完全不同的。让我们通过一个示例来理解局部变量:

文件: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

全局变量

全局变量是在函数外部声明的变量。这些变量可以在程序的任何地方访问。要在函数内部访问全局变量,请在变量前使用GLOBAL关键字。然而,这些变量也可以直接在函数外部访问或使用,无需任何关键字。因此,无需使用任何关键字来访问函数外部的全局变量。

让我们通过一个示例来理解全局变量:

示例

文件: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关键字,在函数内部尝试访问全局变量时会产生一个错误,提示变量未定义。

示例

文件: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

在函数内部使用全局变量的另一种方式是预定义的$GLOBALS数组。

示例:

文件: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有相同的名称,那么在函数内,局部变量比全局变量具有更高的优先级。

示例:

文件:global_variable2.php

<?php
    x = 5;
    function mytest()
    {x = 7;
        echo "value of x: " .$x;
    }
    mytest();
?>

输出:

Value of x: 7

注意:本地变量优先于全局变量。

静态变量

PHP有一个特性,即在变量执行完毕并释放内存后会删除变量。有时候,我们需要在函数执行完毕后仍然保留变量。因此,变量作用域的另一个重要特性是静态变量。我们在变量前面使用static关键字定义一个变量,这个变量被称为 静态变量

静态变量只存在于局部函数中,并且在程序执行离开作用域后不释放其内存。通过以下示例来理解:

示例

文件: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

你必须注意,在每次函数调用后,num1会定期递增,而num2不会。这是因为$num1不是一个静态变量,所以它在每次函数调用后释放了内存。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程