开发者

List all PHP variables

Is it possible to dump all global variables in a PHP script? Say this is my code:

<?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 al开发者_如何学Cl defined constants in a PHP script.


Use get_defined_vars and/or get_defined_constants

$arr = get_defined_vars();
print_r($arr);


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...

<?php
$everything = get_defined_vars();
ksort($everything);

?>

Edit: had to come back to this and realized I had a better answer, $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 and you have access to the php.ini file you can adjust the memory use as so:

ini_set('memory_limit', '1024M');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜