Home >Backend Development >PHP Tutorial >Use get_defined_vars() to get all variables in the current scope

Use get_defined_vars() to get all variables in the current scope

WBOY
WBOYOriginal
2023-06-27 08:57:121241browse

In PHP development, we often need to obtain all variables within the current code scope. These variables may be defined in the current script or referenced from other files or functions. In this case, the PHP built-in function get_defined_vars() is very useful.

get_defined_vars() function returns an array containing all defined variables in the current scope. This includes all global variables, local variables, system variables, etc., and these variables are arranged in the order in which they appear in the global scope. The following is the syntax of the get_defined_vars() function:

array get_defined_vars ( void )

This function does not take any parameters and only needs to be called. The following is a simple example showing how to use this function:

function test() {
   $name = "Tom";
   $age = 25;
   $vars = get_defined_vars();
   echo "The variables in the current scope are:
";
   print_r($vars);
}

test();

Output:

The variables in the current scope are:
Array
(
    [name] => Tom
    [age] => 25
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [_SERVER] => Array
        (
            [HTTP_HOST] => localhost
            [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
            [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
            [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.5
            [HTTP_ACCEPT_ENCODING] => gzip, deflate
            [HTTP_CONNECTION] => keep-alive
            [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
            [PATH] => C:Program Files (x86)Common FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShell1.0;C:WindowsSystem32OpenSSH;C:ProgramDataComposerSetupin;C:Program FilesGitcmd;C:Program Filesdotnet;C:Program FilesMicrosoft SQL ServerXToolsBinn;C:UsersmahmoodAppDataLocalProgramsPythonPython39Scripts;C:UsersmahmoodAppDataLocalProgramsPythonPython39;C:UsersmahmoodAppDataLocalMicrosoftWindowsApps;
            [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
            [TEMP] => C:UsersmahmoodAppDataLocalTemp
            [TMP] => C:UsersmahmoodAppDataLocalTemp
            [WINDIR] => C:Windows
            [USERPROFILE] => C:Usersmahmood
            [PSModulePath] => C:Program FilesWindowsPowerShellModules;C:Windowssystem32WindowsPowerShell1.0Modules
            [PUBLIC] => C:UsersPublic
            [SESSIONNAME] => Console
            [SystemDrive] => C:
            [SystemRoot] => C:Windows
            [ComSpec] => C:Windowssystem32cmd.exe
            [ProgramData] => C:ProgramData
            [ProgramFiles] => C:Program Files
            [ProgramFiles(x86)] => C:Program Files (x86)
            [ProgramW6432] => C:Program Files
            [OneDrive] => C:UsersmahmoodOneDrive
            [CommonProgramFiles] => C:Program FilesCommon Files
            [CommonProgramFiles(x86)] => C:Program Files (x86)Common Files
            [CommonProgramW6432] => C:Program FilesCommon Files
            [PROCESSOR_IDENTIFIER] => Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
            [PROCESSOR_ARCHITECTURE] => AMD64
            [PROCESSOR_ARCHITEW6432] => AMD64
            [NUMBER_OF_PROCESSORS] => 8
            [OS] => Windows_NT
            [USERDOMAIN] => DESKTOP-890S6TR
            [USERNAME] => mahmood
            [USERDNSDOMAIN] => desktop-890s6tr.lan
            [ORIGINAL_PATH] => C:Program Files (x86)Common FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShell1.0;C:WindowsSystem32OpenSSH;C:ProgramDataComposerSetupin;C:Program FilesGitcmd;C:Program Filesdotnet;C:Program FilesMicrosoft SQL ServerXToolsBinn;C:UsersmahmoodAppDataLocalProgramsPythonPython39Scripts;C:UsersmahmoodAppDataLocalProgramsPythonPython39;C:UsersmahmoodAppDataLocalMicrosoftWindowsApps;
            [ORIGINAL_TEMP] => C:UsersmahmoodAppDataLocalTemp
            [ORIGINAL_TMP] => C:UsersmahmoodAppDataLocalTemp
            [ORIGINAL_PATHNAME] => C:Program Files (x86)Common FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShell1.0;C:WindowsSystem32OpenSSH;C:ProgramDataComposerSetupin;C:Program FilesGitcmd;C:Program Filesdotnet;C:Program FilesMicrosoft SQL ServerXToolsBinn;C:UsersmahmoodAppDataLocalProgramsPythonPython39Scripts;C:UsersmahmoodAppDataLocalProgramsPythonPython39;C:UsersmahmoodAppDataLocalMicrosoftWindowsApps;
            [SystemProfile] => C:Windowssystem32configsystemprofile
            [SystemProfileDesktop] => C:Windowssystem32configsystemprofileDesktop
            [PROCESSOR_LEVEL] => 6
            [PROCESSOR_REVISION] => 8c01
            [_] => C:
mppphpphp.exe
        )

    [_SESSION] => Array
        (
        )

    [name] => Tom
    [age] => 25
)

As you can see from the above output, the array returned by the function contains all defined values ​​in the current scope Variables. In the above example, we defined two variables $name and $age, and then used the get_defined_vars() function to get all variables in the current scope. Finally, these variables are displayed by printing the array.

In actual development, using the get_defined_vars() function can greatly improve the efficiency of program development and debugging. It allows us to more easily check the value of the current variable and quickly track the definition and usage of each variable during the development process to ensure the correctness and maintainability of the code.

In short, the get_defined_vars() function is a very practical function in PHP, which can easily obtain all defined variables in the current scope. If you are a PHP developer and haven't used this function yet, I recommend you try it and implement it into your project.

The above is the detailed content of Use get_defined_vars() to get all variables in the current scope. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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