Is there a function to display the defined variables?

Asked

Viewed 396 times

12

There is a way to give a "var_dump" in all instantiated variables in that context, without having to put each one inside the var_dump()?

  • You have the Xdebug installed?

2 answers

12


get_defined_vars(): takes all variables defined in scope(Gobal or local depending on the location of the function call) and then you can run a print_r or var_dump to view their respective values. The value of the superglobals $_POST, $_GET, $_FILE, $_SERVER, $_COOKIE, etc..

Global scope:

<?php
$nome = 'joão';
$idade = 30;
$profissao = 'programador';
$itens = array(1,2,3,4,5,6);

$arr = get_defined_vars();

Local scope:

<?php
function foo(){
    $local = 'exibe apenas a variavel $local';
    $arr = get_defined_vars();

    echo '<pre>';
    print_r($arr);
}

foo();

Example

  • Perfect, was worth beast!

7

Beyond the function get_defined_vars(), quoted in @lost’s reply, in certain situations there is another that can be very useful.

The function get_defined_constants() displays all the defined constants and their values.

  • Cool, I’ll write it down too!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.