Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box
示例 #1
get_defined_vars()
示例
<?php
$b
= array(
1
,
1
,
2
,
3
,
5
,
8
);
$arr
=
get_defined_vars
();
// 打印 $b
print_r
(
$arr
[
"b"
]);
/* 打印 PHP 解释器的路径(如果用于 CGI)
* 例如 /usr/local/bin/php */
echo
$arr
[
"_"
];
// 打印命令行参数(如果有的话)
print_r
(
$arr
[
"argv"
]);
// 打印所有服务器变量
print_r
(
$arr
[
"_SERVER"
]);
// 打印变量数组的所有可用键
print_r
(
array_keys
(
get_defined_vars
()));
?>
cmswares dot com at gmail dot com
¶
3 years ago
Be aware of what get_defined_vars() function returns in different contexts:
- In global scope, a list of all defined variables, whether superglobals, command line arguments or user-defined variables
- In function scope, only a list of user-defined variables (both arguments and in-body definitions)
- In class/object method scope, does not return class/object properties;
Also, as of PHP 5.4, does not return $_ENV even where available.
For further details and scope tests/results, see
https://github.com/php/doc-en/issues/1317
antonfedonyuk at gmail dot com
¶
4 years ago
NOTE: when you call code by eval() PHP append self vars to result, they have "__" prefix ("__source_code" and "__bootstrap_file" in PHP 7.3).
Fix:
<?php
function
filter_eval_vars
(array
$vars
): array
{
foreach (
$vars
as
$key
=>
$value
) {
if (
$key
[
0
] ===
'_'
&&
$key
[
1
] ===
'_'
) {
unset(
$vars
[
$key
]);
}
}
return
$vars
;
}
?>
zabmilenko at hotmail dot com
¶
18 years ago
A little gotcha to watch out for:
If you turn off RegisterGlobals and related, then use get_defined_vars(), you may see something like the following:
<?php
Array
(
[
GLOBALS
] => Array
(
[
GLOBALS
] => Array
*
RECURSION
*
[
_POST
] => Array()
[
_GET
] => Array()
[
_COOKIE
] => Array()
[
_FILES
] => Array()
)
[
_POST
] => Array()
[
_GET
] => Array()
[
_COOKIE
] => Array()
[
_FILES
] => Array()
)
?>
Notice that $_SERVER isn't there. It seems that php only loads the superglobal $_SERVER if it is used somewhere. You could do this:
<?php
print
'<pre>'
.
htmlspecialchars
(
print_r
(
get_defined_vars
(),
true
)) .
'</pre>'
;
print
'<pre>'
.
htmlspecialchars
(
print_r
(
$_SERVER
,
true
)) .
'</pre>'
;
?>
And then $_SERVER will appear in both lists. I guess it's not really a gotcha, because nothing bad will happen either way, but it's an interesting curiosity nonetheless.
ryanlwh at yahoo dot com
¶
12 years ago
Since get_defined_vars() only gets the variables at the point you call the function, there is a simple way to get the variables defined within the current scope.
<?php
$vars
=
get_defined_vars
();
$foo
=
'foo'
;
$bar
=
'bar'
;
$vars
=
array_diff
(
get_defined_vars
(),
$vars
);
echo
'<pre>'
;
print_r
(
$vars
);
echo
'</pre>'
;
?>
Qiibow
¶
3 years ago
get_defined_vars() is very useful for importing many values at once
into another scope (Such as User-defined functions).
Below is an example for showing some of many values and their variable-names in a table.
(useful in debugging)
You can put this user-defined function at many places in your script
to show some values (values to be changed in loops)
to check if they are what they shall be there or not.
<?php
function
get_value_table
(
$name_array
,
$gdv
) {
$name_value_table
= [];
foreach (
$name_array
as
$name
) :
if (!
array_key_exists
(
$name
,
$gdv
)) :
$value
=
'undefined'
;
elseif (
is_bool
(
$gdv
[
$name
])) :
$value
=
$gdv
[
$name
]?
'true'
:
'false'
;
elseif (
is_numeric
(
$gdv
[
$name
]) ||
is_string
(
$gdv
[
$name
])) :
$value
=
$gdv
[
$name
];
elseif (
is_array
(
$gdv
[
$name
])) :
$value
=
'<pre>'
.
print_r
(
$gdv
[
$name
],
true
).
'</pre>'
;
else :
$value
= (
PHP_VERSION_ID
>=
80000
)?
get_debug_type
(
$gdv
[
$name
]) :
get_type
(
$gdv
[
$name
]);
endif;
$name_value_table
[] =
'<tr><td>$'
.
$name
.
'<td>'
.
$value
;
endforeach;
return
'<table border=1>'
.
implode
(
"\n"
,
$name_value_table
).
'</table>'
;
}
$_1
=
'a'
;
$_2
=
'b'
;
$_3
= [
1
,
2
];
$_4
=
false
;
$_5
=
null
;
$name_array
= [
'_1'
,
'_2'
,
'_3'
,
'_4'
,
'_5'
,
'_6'
];
$show_id
=
1
;
if (
$show_id
===
1
) :
echo
get_value_table
(
$name_array
,
get_defined_vars
());
endif;
$_2
=
'c'
;
if (
$show_id
===
2
) :
echo
get_value_table
(
$name_array
,
get_defined_vars
());
endif;
?>
Joey
¶
7 years ago
I occasionally use this as a hack to convert arguments to an array where terseness is needed (dealing with legacy code, etc).
However in an object context it'll also pull in $this.
Make sure to unset it. If you map it to properties or an array then setting the key this wont be an error.
I recommend using a wrapper function that strips this from the result.
lbowerh at adelphia dot net
¶
21 years ago
Here is a function which generates a debug report for display or email
using get_defined_vars. Great for getting a detailed snapshot without
relying on user input.
<?php
function
generateDebugReport
(
$method
,
$defined_vars
,
$email
=
"undefined"
){
$ignorelist
=array(
"HTTP_POST_VARS"
,
"HTTP_GET_VARS"
,
"HTTP_COOKIE_VARS"
,
"HTTP_SERVER_VARS"
,
"HTTP_ENV_VARS"
,
"HTTP_SESSION_VARS"
,
"_ENV"
,
"PHPSESSID"
,
"SESS_DBUSER"
,
"SESS_DBPASS"
,
"HTTP_COOKIE"
);
$timestamp
=
date
(
"m/d/y h:m:s"
);
$message
=
"Debug report created
$timestamp
\n"
;
global
$link
;
$sql_error
=
mysql_error
(
$link
);
if(
$sql_error
){
$message
.=
"\nMysql Messages:\n"
.
mysql_error
(
$link
);
}
foreach(
$defined_vars
as
$key
=>
$val
){
if(
is_array
(
$val
) && !
in_array
(
$key
,
$ignorelist
) &&
count
(
$val
) >
0
){
$message
.=
"\n
$key
array (key=value):\n"
;
foreach(
$val
as
$subkey
=>
$subval
){
if(!
in_array
(
$subkey
,
$ignorelist
) && !
is_array
(
$subval
)){
$message
.=
$subkey
.
" = "
.
$subval
.
"\n"
;
}
elseif(!
in_array
(
$subkey
,
$ignorelist
) &&
is_array
(
$subval
)){
foreach(
$subval
as
$subsubkey
=>
$subsubval
){
if(!
in_array
(
$subsubkey
,
$ignorelist
)){
$message
.=
$subsubkey
.
" = "
.
$subsubval
.
"\n"
;
}
}
}
}
}
elseif(!
is_array
(
$val
) && !
in_array
(
$key
,
$ignorelist
) &&
$val
){
$message
.=
"\nVariable "
.
$key
.
" = "
.
$val
.
"\n"
;
}
}
if(
$method
==
"browser"
){
echo
nl2br
(
$message
);
}
elseif(
$method
==
"email"
){
if(
$email
==
"undefined"
){
$email
=
$_SERVER
[
"SERVER_ADMIN"
];
}
$mresult
=
mail
(
$email
,
"Debug Report for "
.
$_ENV
[
"HOSTNAME"
].
""
,
$message
);
if(
$mresult
==
1
){
echo
"Debug Report sent successfully.\n"
;
}
else{
echo
"Failed to send Debug Report.\n"
;
}
}
}
?>
npelov at croler dot net
¶
4 years ago
get_defined_vars() returns all variables - locally defined vars and global vars (well actually only super globals). If you need local vars only - for example you need to get variables from a file - let's say config.php and you don't want a missing value to be replaced with a global defined somewhere else.
<?php
function
removeGlobals
(array
$localVars
) {
return
array_diff_key
(
$localVars
,
$GLOBALS
);
}
define
(
'CONFIG_FILE_PATH'
,
'/path/to/config.php'
);
function
readConfig
() {
require
CONFIG_FILE_PATH
;
$config
=
removeGlobals
(
get_defined_vars
());
return
$config
;
}
?>
Joey
¶
7 years ago
Some comments here point out that this function wont return references. It does however return names and names are "references".
I would not recommend the suggestions here that convert it to references.
Instead...
public function x($a, $b, $c) {
foreach(array_keys(get_defined_vars()) as $key)
if($key !== 'this')
$this->y(${$key});
}
public function y(&$input) {
$input++;
}
Instead of ${} you can also use $$.
I have done some whacky things in my time to make extremely generic code but I've never had to do anything like the above. It might not even work (but should since it's no different to $a[$key]).
You could also do $$key++ but I've never seen code like that which wasn't horrifically bad (using dynamic where dynamic isn't beneficial).
If you're doing something like that then give it additional scrutiny.
jgettys at gnuvox dot com
¶
23 years ago
Simple routine to convert a get_defined_vars object to XML.
<?php
function
obj2xml
(
$v
,
$indent
=
''
) {
while (list(
$key
,
$val
) =
each
(
$v
)) {
if (
$key
==
'__attr'
) continue;
if (
is_object
(
$val
->
__attr
)) {
while (list(
$key2
,
$val2
) =
each
(
$val
->
__attr
)) {
$attr
.=
"
$key2
=\"
$val2
\""
;
}
}
else
$attr
=
''
;
if (
is_array
(
$val
) ||
is_object
(
$val
)) {
print(
"
$indent
<
$key$attr
>\n"
);
obj2xml
(
$val
,
$indent
.
' '
);
print(
"
$indent
</
$key
>\n"
);
}
else print(
"
$indent
<
$key$attr
>
$val
</
$key
>\n"
);
}
}
$x
->
name
->
first
=
"John"
;
$x
->
name
->
last
=
"Smith"
;
$x
->
arr
[
'Fruit'
] =
'Bannana'
;
$x
->
arr
[
'Veg'
] =
'Carrot'
;
$y
->
customer
=
$x
;
$y
->
customer
->
__attr
->
id
=
'176C4'
;
$z
=
get_defined_vars
();
obj2xml
(
$z
[
'y'
]);
?>
will output:
<customer id="176C4">
<name>
<first>John</first>
<last>Smith</last>
</name>
<arr>
<Fruit>Bannana</Fruit>
<Veg>Carrot</Veg>
</arr>
</customer>
istiaquealamjoy at gmail dot com
¶
8 years ago
for all those who don't know how to us this function. copy and run this code in your computer .
note: you need to know what superglobals are.....
<?php
$A
=
5
;
$B
=
10
;
$C
=
15
;
$D
=
20
;
$F
=
get_defined_vars
(
$A
);
var_dump
(
$F
);
?>
Result : will show all superglobals status along with your defined
variables along with its values
SyCo
¶
16 years ago
Here's a very simple function for debugging. It's far from perfect but I find it very handy. It outputs the var value and the var name on a new line. The problem is it'll echo any vars and their name if they share the same value. No big deal when debugging and saves the hassle of writing the HTML and var name when echoing a variable. (ev=echo variable). Using get_defined_vars() inside a function renames the var name to the functions variable so isn't as useful for debugging. Of course, you'll need access to the $GLOBALS array
<?
function ev($variable){
foreach($GLOBALS as $key => $value){
if($variable===$value){
echo '<p>'.$key.' - '.$value.'</p>';
}
}
}
$a=0;
ev($a);
$b=0;
ev($b);
$c=0;
ev($c);
?>
Will output
a - 0
a - 0
b - 0
a - 0
b - 0
c - 0
donovan at example dot com
¶
16 years ago
As a note, get_defined_vars() does not return a set of variable references (as I hoped). For example:
<?php
$my_var
=
"foo"
;
$defined_vars
=
get_defined_vars
();
$defined_vars
[
"my_var"
] =
"bar"
;
echo
$my_var
,
"\n"
;
?>
will output "foo" (the original value). It'd be nice if get_defined_vars() had an optional argument to make them references, but I imagine its a rather specialized request. You can do it yourself (less conveniently) with something like:
<?php
$defined_vars
= array();
$var_names
=
array_keys
(
get_defined_vars
());
foreach (
$var_names
as
$var_name
)
{
$defined_vars
[
$var_name
] =& $
$var_name
;
}
?>
Joey
¶
7 years ago
I posted here before about "this" being in get_defined_vars.
It turns out it's not always there but in certain cases it will inexplicably appear.
php -r '
class Test {
public function a() {var_dump(array_keys(get_defined_vars()));$a = 123;}
public function b() {var_dump(array_keys(get_defined_vars()));$this;}
}
$t = new Test();
$t->a();
$t->b();
'
array()
array('this')
This does not happen in PHP 7.2 but will happen in PHP 5.6.
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.
Johan de Vries
¶
14 years ago