Dumping All Variables and Constants in PHP
Attempting to retrieve every variable currently in scope within a PHP script is a common request when debugging complex state or analyzing application memory usage. The most straightforward approach for global variables relies on the get_defined_vars() function. This built-in utility returns an associative array containing all local and global variables currently accessible in the current execution context.
If you invoke this function at the global level, it captures everything visible to that specific scope. In your example where $foo and $bar are defined before including a file, calling get_defined_vars() after the include statement will yield an array containing both your initial variables and any new ones introduced by blah.php. The function does not require arguments and returns the state of the symbol table as it exists at that exact moment.
<?php
$foo = 1;
$bar = "2";
include("blah.php");
$allVars = get_defined_vars();
print_r($allVars);
// Output includes $foo, $bar, and variables from blah.php
?>
However, there is a critical distinction between global variables and local variables that often trips up developers. If you call get_defined_vars() inside a function, it only returns the variables defined within that specific function's scope, excluding global variables unless they are explicitly passed or accessed via the $GLOBALS superglobal array. To truly dump all variables regardless of where they were defined in the stack trace, you must ensure the call happens outside any function, or you manually iterate through get_defined_vars() for every active function if deep inspection is required.
The Global Symbol Table Limitation
While get_defined_vars() is convenient, it has limitations when dealing with symbols registered at a lower level than standard variables. Specifically, constants defined using the older define() function do not appear in the array returned by get_defined_vars(). This is because constants are stored in the global symbol table but are treated distinctly from variables during execution.
To dump all defined constants, including those set via define() and potentially const, you need to query the global constant registry directly. The function get_defined_constants() serves this purpose. It returns an associative array where keys represent constant names and values represent their current values. This is distinct from variables because it operates on the definition table rather than the variable table.
<?php
define('MY_CONST', 100);
$var = 'data';
// Variables only
$vars = get_defined_vars(); // Does not show MY_CONST
// Constants only
constMap = get_defined_constants(true);
print_r(constMap['Core'] ?? []); // Shows constants from all loaded extensions
?>
Using get_defined_constants(true) returns a more detailed structure. The first argument is optional but highly recommended for specific needs. If you pass true, the function returns an associative array with two keys: 'core' and 'user'. The 'core' key holds constants from PHP's built-in extensions, while 'user' contains constants defined by your application or loaded extensions. This separation is vital when trying to isolate custom application constants from framework or system-defined ones.
Handling Super Globals and Pitfalls
When analyzing the full state of a script, you must also consider superglobals like $_GET, $_POST, $_SERVER, and $_SESSION. These are technically global variables, so they will be included in the output of get_defined_vars() if accessed at the global scope. However, accessing them inside a function without declaring them as static or using $GLOBALS can lead to confusion regarding their presence in the returned array.
A common pitfall is assuming that get_defined_vars() provides a complete snapshot of memory usage. It only lists variables currently bound to symbols. Resources, open file handles, or objects holding large data structures might not be immediately obvious just from the variable names unless you inspect their properties individually. Furthermore, if your script uses dynamic class instantiation or closures that capture context, those internal states remain hidden unless explicitly traversed.
For production debugging where comprehensive state analysis is needed, relying solely on get_defined_vars() and get_defined_constants() provides a solid foundation, but remember that these functions reflect the runtime environment exactly as it is at the call site. They do not preserve history or provide stack traces of variable creation. If you need to persist this data for later analysis, serialize the returned arrays immediately, as the script state changes with every new line executed.
If you are building a framework or debugging tool within the Laravel ecosystem, you might find similar utility functions helpful for inspecting request scope, though PHP's native functions remain the standard for raw variable inspection. Always verify whether your specific use case requires checking extension-defined constants separately from user-defined ones to ensure accurate diagnostics.