简体   繁体   English

转储PHP中的许多变量

[英]Dumping many variables in PHP

I assure this is a very common situation when you have to test variables (and they are many!), just like this example (I only named vars like this for less-effort-writing sake): 我保证当您必须测试变量时(这是很多!),这是一个非常普遍的情况,就像下面的示例一样(为方便起见,我只这样命名vars):

    $variable0='red';
    $variable1='blue';
    $variable2='green';
    $variable3='pink';
    $variable4='purple';
    $variable5='hellow';
    $variable6='foo';
    $variable7='bar';
    $variable8='hi';
    $variable9='bye';

    echo
    '$variable0='.$variable0.'<br>
$variable1='.$variable1.'<br>
$variable2='.$variable2.'<br>
$variable3='.$variable3.'<br>
$variable4='.$variable4.'<br>
$variable5='.$variable5.'<br>
$variable6='.$variable6.'<br>
$variable7='.$variable7.'<br>
$variable8='.$variable8.'<br>
$variable9='.$variable9;

My question is: is there a better way to make this echoing / dumping / printing easier? 我的问题是:是否有更好的方法使回显/转储/打印更容易?

Of course, there are other ways of doing the very same presidiary work: 当然,还有其他方法可以完成相同的常规工作:

        $x=
    '$variable0='."$variable0\n".
    '$variable1='."$variable1\n".
    '$variable2='."$variable2\n".
    '$variable3='."$variable3\n".
    '$variable4='."$variable4\n".
    '$variable5='."$variable5\n".
    '$variable6='."$variable6\n".
    '$variable7='."$variable7\n".
    '$variable8='."$variable8\n".
    '$variable9='."$variable9"
    echo nl2br($x);

Or: 要么:

    $x=<<<HEREDOC
\$variable1=$variable1
\$variable2=$variable2
\$variable3=$variable3
\$variable4=$variable4
\$variable5=$variable5
\$variable6=$variable6
\$variable7=$variable7
\$variable8=$variable8
\$variable9=$variable9;
HEREDOC;
    echo nl2br($x);

But maybe PHP has a function to make this easier? 但是,也许PHP具有使此操作更容易的功能?

By the way, all 3 solutions above echoes the very same: 顺便说一句,以上所有3个解决方案都是相同的:

$variable1=blue<br>
$variable2=green<br>
$variable3=pink<br>
$variable4=purple<br>
$variable5=hellow<br>
$variable6=foo<br>
$variable7=bar<br>
$variable8=hi<br>
$variable9=bye;

Introducing compact : compact介绍:

var_dump(compact('foo', 'bar', 'baz'));

Note though that I explicitly used three very different variables: $foo , $bar and $baz . 请注意,尽管我明确使用了三个非常不同的变量: $foo$bar$baz
If you actually do literally have $foo1 , $foo2 etc, you're really really looking to use an array instead. 如果您确实确实有$foo1$foo2等,那么您实际上在寻找使用数组。 Dumping that would be trivial too: 倾销也将是微不足道的:

$foo    = array();
$foo[0] = 'bar';
$foo[1] = 'baz';
var_dump($foo);

In general, if you have too many variables floating around, your scope is probably too big and you should refactor everything into a number of smaller functions, or your algorithm is more complex than it needs to be, or you should be using arrays or other data structures instead. 通常,如果周围有太多变量,则范围可能太大,应将所有内容重构为多个较小的函数,或者算法比所需的复杂,或者应使用数组或其他数据结构。

get_defined_vars() would be able to do so. get_defined_vars()可以做到这一点。 Might be overkill ( all accessable vars will be shown): 可能过大(将显示所有可访问的变量):

print_r( get_defined_vars() );

Return Values: A multidimensional array with all the defined variables*. 返回值:具有所有已定义变量*的多维数组。
*In a function it will only show the local $vars, and those defined as global. *在一个函数中,它将仅显示本地$ vars和定义为global的$ vars。

Note: This does exactly what you're looking for, but the array methods mentioned in other answers would be a better fit logical-wise, see below: 注意:这正是您要寻找的,但是其他答案中提到的数组方法将更符合逻辑,请参见下文:


A better way for you to set the values is with an array, this way you 'group' multiple values together: 设置值的更好方法是使用数组,这样可以将多个值“分组”在一起:

$color[] = 'red'; // will automatically start with key=0
$color[] = 'blue'; // key=1
$color[] = 'green';// key=2 etc
print_r( $color );
echo $color[1]; // Blue, same as your echo $variable1;

You can declare your variables as keys from an array instead 您可以改为将变量声明为数组中的键

$myarray=Array();

$myarray['variable0']='red';
$myarray['variable1']='blue';
$myarray['variable2']='green';
$myarray['variable3']='pink';
$myarray['variable4']='purple';
$myarray['variable5']='hellow';
$myarray['variable6']='foo';
$myarray['variable7']='bar';
$myarray['variable8']='hi';
$myarray['variable9']='bye';

and print them all with 并用它们全部打印

echo '<pre>';
print_r($myarray);
echo '</pre>';

then, in case you still need to use them as separate variables, doing 然后,如果您仍然需要将它们用作单独的变量,请执行

extract($myarray)

will create $variable0 to $variable9 in the global context. 将在全局上下文中创建$ variable0到$ variable9。

如果您知道上限和下限,并且所有变量都具有相似的名称,则可以执行以下操作:

for ($i = 0; $i < $end; ++$i) { echo ${'value_name'. $i}; }

Try refactor your code and use arrays 尝试重构您的代码并使用数组

Then you can do print_r or var_dump 然后您可以执行print_r或var_dump

This should give you the output your looking for: 这应该为您提供所需的输出:

function get_var_name($var) {
    foreach($GLOBALS as $var_name => $value) {
        if ($value === $var) {
            return $var_name;
        }
    }

    return "Variable name not found";
}

function miracle_var_dump($valuesToDump) {

    //For each variable in the $valuesToDump array, print the name and value
    for($i = 0; $i < count($valuesToDump); $i++) {
        echo(get_var_name($valuesToDump[i]) . "=" . $valuesToDump[i] . "<br/>");
    }

}

//Making a call to dump the variables
miracle_var_dump(array($variable0,$variable1,$variable2,$variable3,$variable4,$variable5,$variable6,$variable7,$variable8,$variable9))

Function get_var_name gets the variable name given the variable (taken from one of Jeremey Rutins awsners ). 函数get_var_name获取给定变量的变量名(取自Jeremey Rutins awsners之一 )。

//let your variables like this
$variable0='red';
$variable1='blue';
$variable2='green';

//an array to keep all the variable names
$var_names=array();
$var_names[]='$variable0';
$var_names[]='$variable1';
$var_names[]='$variable2';
//etc...

print_var_name($var_names);

function print_var_name($var_names) {
//loop through the array and dump them if they are in $GLOBALS
foreach($GLOBALS as $var_name => $value) {
    if (in_array('$'.$var_name,$var_names)) {
        echo $var_name;
        var_dump($value);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM