-1

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):

    $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?

By the way, all 3 solutions above echoes the very same:

$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;
Terkwood
  • 39
  • 2
  • 6
Roger
  • 8,286
  • 17
  • 59
  • 77
  • 6
    What is uncommon about this situation is your use of so many variables in the first place. Nobody writes code like that. Use an array to store the values, and `var_dump` the array. – user229044 Aug 22 '14 at 14:04
  • +1 ^^^ - plus adding a count to automatically increase and concatenate from variable `0` right up to God knows where; a timesaver. – Funk Forty Niner Aug 22 '14 at 14:07
  • 1
    Dumping vars is dumb by way of mirroring the p and in general too. Use a debugger, set a breakpoint and look at es many variables as you like. – Armin Aug 22 '14 at 14:08
  • 4
    Your "dream" function is trivially easy to implement yourself, but also *awful* to use. You *still* have to type out all those variable names. Just **use an array**. – user229044 Aug 22 '14 at 14:12
  • Okay, "the dream is over". However, the PHP array syntax is equally awful... – Roger Aug 22 '14 at 14:42

7 Answers7

5

Introducing compact:

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

Note though that I explicitly used three very different variables: $foo, $bar and $baz.
If you actually do literally have $foo1, $foo2 etc, you're really really looking to use an array instead. 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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • If I will keep the variables approach your solution is the best: `$foo=1; $bar=2; print_r(compact('foo','bar'));` – Roger Aug 22 '14 at 14:21
  • @Martijn I'd really hope you're never in a situation where you need to dump 10 or more variables to debug something... :) – deceze Aug 22 '14 at 14:48
  • Not normal variables, no. An array easily, but that's a LOT easier anyways :) – Martijn Aug 22 '14 at 17:33
4

get_defined_vars() would be able to do so. 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.

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;
Martijn
  • 15,791
  • 4
  • 36
  • 68
2

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.

ffflabs
  • 17,166
  • 5
  • 51
  • 77
1

If you know the upper and lower bounds and all the variables have a similar name you could do the following:

for ($i = 0; $i < $end; ++$i) { echo ${'value_name'. $i}; }
NaeiKinDus
  • 730
  • 20
  • 30
  • That's the idea I had too, as per [`my comment`](http://stackoverflow.com/questions/25448816/dumping-many-variables-in-php#comment39707519_25448816) – Funk Forty Niner Aug 22 '14 at 14:09
  • Yup, didn't see it before posting my reply, I was on my phone and it took me a while to type that :) "Great minds think alike" it seems. – NaeiKinDus Aug 22 '14 at 14:11
  • 2
    Yes, and I think it's exactly the reason why @deceze wrote "you're really really looking to use an array instead" in its answer. Variable variables aren't that good and it's not really a good use case here. – Clément Malet Aug 22 '14 at 14:11
  • @NaeiKinDus Don't worry about it :) I was just thinking outloud. I like the idea; could be useful for the OP or for someone else visiting the question. – Funk Forty Niner Aug 22 '14 at 14:12
  • @ClémentMalet I completely agree but I was astonished that few PHP developers I work(ed) with knew about that. This method can prove itself useful, albeit not recommended. – NaeiKinDus Aug 22 '14 at 14:23
0

Try refactor your code and use arrays

Then you can do print_r or var_dump

  • 1
    Hey, welcome to SO. Next time add a little example code (i.e. an array version of his code), to make the answer speak a bit more for itself. When you're new to coding, `array`, `print_r` and `var_dump` just sound like scary stuff :) – Martijn Aug 22 '14 at 14:23
0

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).

Community
  • 1
  • 1
Antoine Dahan
  • 574
  • 2
  • 9
  • 23
  • **Please** add a break after the return on line 5. Wasted precious resources :) Apart from that, this is the complex version of `compact()` :) – Martijn Aug 22 '14 at 14:30
  • The break? You're looking through the array untill you get a match. But after thatr you keep going, while you found one. ALso, two vars with the same value will err :) – Martijn Aug 22 '14 at 17:34
  • After the return, the code will resume where the function was invoked, it won't stay in the loop. Read [this](http://php.net/manual/en/function.return.php). – Antoine Dahan Aug 22 '14 at 18:19
0
//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);
    }
}
Buddhi
  • 2,224
  • 5
  • 32
  • 43
  • This will not work on multidimentional arrays, and is a complex version of `get_defined_vars()` :) – Martijn Aug 22 '14 at 14:31