Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in php is there a way to dump "all" variable names with their corresponding value?

Tags:

php

debugging

if the title seems too vague.. uhm i wanted to display every variable that i used to generate a page along with their variable names and values, is it possible and how?

    foreach($_SESSION as $varname => $value) {
        print "<b>".$varname."</b> = $value <br/>";
    }

^the above sample is what i use to display all session variables, what if i need to display the variables i set to display the page? are they registered also in some form of an array or should i also echo them individually?

like image 740
lock Avatar asked Jun 17 '09 03:06

lock


3 Answers

You can use get_defined_vars() which will give you an array of all variables declared in the scope that the function is called including globals like $_SESSION and $_GET. I would suggest printing it like so:

echo '<pre>' . print_r(get_defined_vars(), true) . '</pre>';
like image 138
rojoca Avatar answered Sep 21 '22 15:09

rojoca


The easiest way to do this is with get_defined_vars().

From the Documentation

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

Producing a var_dump of this array will provide you with an extensive list.

var_dump( get_defined_vars() );
like image 24
Josiah Avatar answered Sep 25 '22 15:09

Josiah


A couple other interesting functions in the same area are get_defined_constants() and get_included_files()...

like image 26
grantwparks Avatar answered Sep 22 '22 15:09

grantwparks