Solved

All variables

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

759 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

23 Experts available now in Live!

Get 1:1 Help Now