列出所有 PHP 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5101942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List all PHP variables
提问by Salman A
Is it possible to dump all global variables in a PHP script? Say this is my code:
是否可以在 PHP 脚本中转储所有全局变量?说这是我的代码:
<?php
$foo = 1;
$bar = "2";
include("blah.php");
dumpall();
// displays $foo, $bar and all variables created by blah.php
Also, is it possible to dump all defined constants in a PHP script.
此外,是否可以在 PHP 脚本中转储所有定义的常量。
回答by nico
Use get_defined_vars
and/or get_defined_constants
使用get_defined_vars
和/或get_defined_constants
$arr = get_defined_vars();
print_r($arr);
回答by John
When debugging trying to find differences using a program such as WinMerge(freeware) to see what differences various arrays and variables have you'll want to ksort()
otherwise you'll get lots of false negatives. It also helps to visually format using the HTML pre
element...
当调试尝试使用诸如WinMerge(免费软件)之类的程序查找差异以查看各种数组和变量之间的差异时,您会想要,ksort()
否则您会得到很多误报。它还有助于使用 HTMLpre
元素直观地格式化...
<?php
$everything = get_defined_vars();
ksort($everything);
?>
Edit: had to come back to this and realized I had a better answer, $GLOBALS
.
编辑:不得不回到这个问题并意识到我有一个更好的答案,$GLOBALS
.
$a = print_r(var_dump($GLOBALS),1);
echo '<pre>';
echo htmlspecialchars($a);
echo '</pre>';
Edit 2: as mpag mentioned print_r()
may be susceptible to running out of memory if the software you're working with uses a lot. Presuming there is no output or it's clearly truncated andyou have access to the php.ini
file you can adjust the memoryuse as so:
编辑 2:如 mpag 提到的print_r()
,如果您使用的软件使用很多,则可能容易耗尽内存。假设没有输出或者它被明显截断并且您可以访问该php.ini
文件,您可以像这样调整内存使用:
ini_set('memory_limit', '1024M');