get_defined_vars() 函數(shù)返回由所有已定義變量所組成的數(shù)組。
版本要求:PHP 4 >= 4.0.4, PHP 5, PHP 7
array get_defined_vars ( void )
參數(shù)說明:
void。
返回一個包含所有已定義變量列表的多維數(shù)組,這些變量包括環(huán)境變量、服務(wù)器變量和用戶定義的變量。
<?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["_"]; // 打印命令行參數(shù)(如果有的話) print_r($arr["argv"]); // 打印所有服務(wù)器變量 print_r($arr["_SERVER"]); // 打印變量數(shù)組的所有可用鍵值 print_r(array_keys(get_defined_vars())); ?>
執(zhí)行結(jié)果如下所示:
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 )