Search code examples
phpwordpressvar-dump

Why is my var_dump() only showing when used outside of the function?


I'm learning about var_dump() while trying to debug some code in my WordPress functions.php file.

When var_dump() is used inside a function it does not display on the page. Nothing will display with this:

function my_function() {
    $test_variable = 'some words';
    var_dump($test_variable);
}

But when var_dump() is outside of a function it displays fine. This displays the dump:

$test_variable = 'some words';
var_dump($test_variable);

Why is my var_dump() only showing when used outside of the function?


Solution

  • You have not called function any where.

    function my_function() {
        $test_variable = 'some words from inside my_function';
        var_dump($test_variable);
    }
    
    $test_variable = 'some words from out side my_function';
    var_dump($test_variable);
    
    my_function();
    

    This show the both statement.