Solved

All variables

Posted on 2013-05-14
2
147 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now