Solved

All variables

Posted on 2013-05-14
2
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Assisted Solution

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

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

628 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