php – Is there a function to display the defined variables?

Question:

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

Answer:

get_defined_vars() : Gets all the variables defined in scope (gobal or local depending on where the function is called) and then you can run a print_r or var_dump to see their respective values. The value of the superglobals $_POST , $_GET , $_FILE , $_SERVER , $_COOKIE , etc. is also returned.

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

Scroll to Top