Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

All variables

Posted on 2013-05-14
2
Medium Priority
?
161 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 1000 total points
ID: 39163871
get_defined_vars
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

886 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