Solved

All variables

Posted on 2013-05-14
2
149 Views
Last Modified: 2013-05-30
Is it possible to list all variables open in a script and their values? Or even better a list of open objects?

I can already list all $_POST, $_GET, $_COOKIES and $_SESSION but would really like to know a list of open variables, mainly for debug but it would be nice to see if there are any that I have not closed down correctly at the end of a page.

Thank you
0
Comment
Question by:tonelm54
2 Comments
 
LVL 10

Assisted Solution

by:ienaxxx
ienaxxx earned 250 total points
ID: 39163871
get_defined_vars
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39164333
Variables do not get "opened" or "closed" -- they get set or unset() and there is rarely a reason to unset() variables at the end of a script.  Variable have a scope of definition associated with the context in which they are defined, which is to say that the symbol table is segmented and the PHP scripts can only get to the segment that is active for their scope.

Install this and run it.  Take careful note of the inconsistent behavior when used in the object constructor.
<?php // RAY_temp_tonelm54.php
error_reporting(E_ALL);
echo '<pre>';

// SEE
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28127115.html
// http://www.php.net/manual/en/function.get-defined-vars.php
// https://bugs.php.net/bug.php?id=52110

// SEE ALSO
// http://www.php.net/manual/en/function.get-defined-functions.php
// http://www.php.net/manual/en/function.get-defined-constants.php
// http://www.php.net/manual/en/function.get-declared-classes.php

// GLOBAL SCOPE
$arr = get_defined_vars();
print_r($arr);

// WITHIN A FUNCTION
function thing($x)
{
    $a = $x;
    $b = $x;
    $arr = get_defined_vars();
    var_dump($arr);
}
thing(5);

// WITHIN AN OBJECT
Class One
{
    private $a = 1;
    protected $b = 2;
    public $c = 3;

    public function __construct()
    {
        $this->data = 'Hello World';
        $arr = get_defined_vars();
        print_r($arr);
    }
}
$x = new One;

// SHOW INCONSISTENCY
var_dump($x);

Open in new window

Best regards, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Query return divisible by 3 3 26
Help in good tutorials for PHP, HTML and CSS 6 41
selector:validator cookies 4 30
Extracting content from meta tag PHP MYHTML DOM 3 21
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question