PHPverse 2025

Voting

: max(two, six)?
(Example: nine)

The Note You're Voting On

Anonymous
14 years ago
get_defined_vars() returns ALL the vars (in the current scope), what if you just want all YOUR vars, not PHP's super-globals?

<?php
var_export
(array_diff(get_defined_vars(), array(array())));
?>

Example...

<?php
$TOP_LEVEL_VAR
=1;
var_export(array_diff(get_defined_vars(), array(array())));
?>

The output (with register_globals off) should be...

array (
'TOP_LEVEL_VAR' => 1,
)

...it perfectly eliminated all the super-globals, without me having to specify them! (note with register_globals on, the output includes those globals, then TOP_LEVEL_VAR).

Here it is, as a function...(it's the best I could do {I can't call get_defined_vars() inside get_user_defined_vars() cuz of the scope issue}).

<?php
header
('Content-type: text/plain');

$TOP_LEVEL_VAR=1;

echo
'register_globals(';
echo
ini_get('register_globals');
echo
') '.phpversion()."\n";

var_export(get_user_defined_vars(get_defined_vars()));

function
get_user_defined_vars($vars) {
return
array_diff($vars, array(array()));
}
?>

Note that originally I had an array of the super-globals I wanted removed from get_defined_vars()'s array, then I noticed even an empty double-array, array(array()), made it give me the correct result. Weird.

This was tested on PHP 5.2.9.

<< Back to user notes page

To Top