get_defined_vars() 函数返回由所有已定义变量所组成的数组。
版本要求:PHP 4 >= 4.0.4, PHP 5, PHP 7
array get_defined_vars ( void )
参数说明:
void。
返回一个包含所有已定义变量列表的多维数组,这些变量包括环境变量、服务器变量和用户定义的变量。
<?php $b = array(1,1,2,3,5,8); $arr = get_defined_vars(); // 打印 $b print_r($arr["b"]); // 打印 PHP 解释程序的路径(如果 PHP 作为 CGI 使用的话) // 例如:/usr/local/bin/php echo $arr["_"]; // 打印命令行参数(如果有的话) print_r($arr["argv"]); // 打印所有服务器变量 print_r($arr["_SERVER"]); // 打印变量数组的所有可用键值 print_r(array_keys(get_defined_vars())); ?>
执行结果如下所示:
Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 8 ) Array ( [0] => /tmp/543750210/main.php ) Array ( [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin [HOSTNAME] => glot-runner [PHPIZE_DEPS] => autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c [PHP_INI_DIR] => /usr/local/etc/php [PHP_CFLAGS] => -fstack-protector-strong -fpic -fpie -O2 [PHP_CPPFLAGS] => -fstack-protector-strong -fpic -fpie -O2 [PHP_LDFLAGS] => -Wl,-O1 -Wl,--hash-style=both -pie [GPG_KEYS] => 1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F [PHP_VERSION] => 7.2.1 [PHP_URL] => https://secure.php.net/get/php-7.2.1.tar.xz/from/this/mirror [PHP_ASC_URL] => https://secure.php.net/get/php-7.2.1.tar.xz.asc/from/this/mirror [PHP_SHA256] => 6c6cf82fda6660ed963821eb0525214bb3547e8e29f447b9c15b2d8e6efd8822 [PHP_MD5] => [HOME] => /home/glot [PHP_SELF] => /tmp/543750210/main.php [SCRIPT_NAME] => /tmp/543750210/main.php [SCRIPT_FILENAME] => /tmp/543750210/main.php [PATH_TRANSLATED] => /tmp/543750210/main.php [DOCUMENT_ROOT] => [REQUEST_TIME_FLOAT] => 1524198667.12 [REQUEST_TIME] => 1524198667 [argv] => Array ( [0] => /tmp/543750210/main.php ) [argc] => 1 ) Array ( [0] => _GET [1] => _POST [2] => _COOKIE [3] => _FILES [4] => argv [5] => argc [6] => _SERVER [7] => b [8] => arr )