有没有办法在PHP中获取用户声明的变量?

Mil*_*vić 13 php

get_defined_vars即将(引用):

返回一个多维数组,其中包含所有已定义变量的列表,可以是环境,服务器或用户定义的变量

好吧,对于我的调试任务,我只需要那些用户定义的.是否内置php或补充功能?

编辑:好的,我没有说清楚我到底是什么,这里是一个小例子:

<?php
/*
this script is included, and I don't have info
about how many scripts are 'above' and 'bellow' this*/


//I'm at line 133
$user_defined_vars = get_user_defined_vars();
//$user_defined_vars should now be array of names of user-defined variables
//what is the definition of get_user_defined_vars()?

?>
Run Code Online (Sandbox Code Playgroud)

eis*_*erg 15

是的你可以:

<?php
// Start
$a = count(get_defined_vars());

/* Your script goes here */
$b = 1;

// End
$c = get_defined_vars();
var_dump(array_slice($c, $a + 1));
Run Code Online (Sandbox Code Playgroud)

将返回:

array(1) {
  ["b"]=>
  int(1)
}
Run Code Online (Sandbox Code Playgroud)


Cri*_*isp 9

一个小阵列操作怎么样?

$testVar = 'foo';
// list of keys to ignore (including the name of this variable)
$ignore = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', '_SERVER', '_ENV', 'ignore');
// diff the ignore list as keys after merging any missing ones with the defined list
$vars = array_diff_key(get_defined_vars() + array_flip($ignore), array_flip($ignore));
// should be left with the user defined var(s) (in this case $testVar)
var_dump($vars);

// Result: 
array(1) {
    ["testVar"]=>string(3) "foo"
}
Run Code Online (Sandbox Code Playgroud)