get_defined_vars()

Description

This function returns a multi-dimensional associative array containing the names and values of all defined variables at the point it is called. This will include environment, server, and user-defined variables that are within scope when the function is called.

Syntax


array get_defined_vars( void )

Parameters

This function takes no arguments.

Return Value

array
Returns a multi-dimensional associative array where the keys correspond to the variable names and the values contain the current value of each associated variable.

Examples

1. Get Defined Variables in a Command-line Environment

This example uses print_r() to print the array of all defined variables from a command-line invocation of the PHP interpreter.

<?php
$definedVars = get_defined_vars();
print_r( $definedVars );
?>

The output shows the array returned by the call to get_defined_vars().

Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [argv] => Array
        (
            [0] => get_defined_vars.php
        )

    [argc] => 1
    [_SERVER] => Array
        (
            [SSH_AGENT_PID] => 1756
            [GPG_AGENT_INFO] => /run/user/charlie/keyring-L7imNQ/gpg:0:1
            [TERM] => xterm
            [SHELL] => /bin/bash
            [XDG_SESSION_COOKIE] => 1a9c386e7ee60823248f3b8752474745-1383051553.610835-632666142
            [WINDOWID] => 65011717
            [GNOME_KEYRING_CONTROL] => /run/user/charlie/keyring-L7imNQ
            [GTK_MODULES] => overlay-scrollbar
            [USER] => charlie
            [LS_COLORS] => rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
            [XDG_SESSION_PATH] => /org/freedesktop/DisplayManager/Session0
            [XDG_SEAT_PATH] => /org/freedesktop/DisplayManager/Seat0
            [SSH_AUTH_SOCK] => /run/user/charlie/keyring-L7imNQ/ssh
            [SESSION_MANAGER] => local/osprey:@/tmp/.ICE-unix/1675,unix/osprey:/tmp/.ICE-unix/1675
            [DEFAULTS_PATH] => /usr/share/gconf/ubuntu.default.path
            [XDG_CONFIG_DIRS] => /etc/xdg/xdg-ubuntu:/etc/xdg
            [PATH] => /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
            [DESKTOP_SESSION] => ubuntu
            [PWD] => /home/charlie/php
            [GNOME_KEYRING_PID] => 1664
            [LANG] => en_US.UTF-8
            [MANDATORY_PATH] => /usr/share/gconf/ubuntu.mandatory.path
            [UBUNTU_MENUPROXY] => libappmenu.so
            [COMPIZ_CONFIG_PROFILE] => ubuntu
            [GDMSESSION] => ubuntu
            [SHLVL] => 1
            [HOME] => /home/charlie
            [GNOME_DESKTOP_SESSION_ID] => this-is-deprecated
            [LOGNAME] => charlie
            [COMPIZ_BIN_PATH] => /usr/bin/
            [XDG_DATA_DIRS] => /usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/
            [DBUS_SESSION_BUS_ADDRESS] => unix:abstract=/tmp/dbus-7Rq4KycTBV,guid=b003e1bf6da4727203aff211526fb122
            [LESSOPEN] => | /usr/bin/lesspipe %s
            [TEXTDOMAIN] => im-config
            [XDG_RUNTIME_DIR] => /run/user/charlie
            [DISPLAY] => :0
            [XDG_CURRENT_DESKTOP] => Unity
            [LESSCLOSE] => /usr/bin/lesspipe %s %s
            [TEXTDOMAINDIR] => /usr/share/locale/
            [COLORTERM] => gnome-terminal
            [XAUTHORITY] => /home/charlie/.Xauthority
            [_] => /usr/bin/php
            [OLDPWD] => /home/charlie
            [PHP_SELF] => get_defined_vars.php
            [SCRIPT_NAME] => get_defined_vars.php
            [SCRIPT_FILENAME] => get_defined_vars.php
            [PATH_TRANSLATED] => get_defined_vars.php
            [DOCUMENT_ROOT] => 
            [REQUEST_TIME_FLOAT] => 1383056588.024
            [REQUEST_TIME] => 1383056588
            [argv] => Array
                (
                    [0] => get_defined_vars.php
                )

            [argc] => 1
        )

)

2. Get Defined Variables in a Web Server Environment

This example uses print_r() to print the array of all defined variables from a web server invocation of the PHP interpreter.

<html>
<body>
  <?php
  $definedVars = get_defined_vars();
  echo "<pre>\n";
  print_r( $definedVars );
  echo "</pre>\n";
  ?>
</body>
</html>

The snapshot below shows the partial browser output of the program. Notice that the _GET array includes the test variable assignment from the query string.

get_defined_vars()

See Also

References