DEV Community

Anders Björkland
Anders Björkland

Posted on

Ho ho Oh no! Where are my variables?

Just the Gist

A simple way of debugging is to use the built-in PHP function var_dump() to print out the contents of a variable. But it's not the only way to do it. Here's a short introduction to the basics of debugging in PHP.

Just outputting the variable

If you want to debug you code, you may want to know what value a variable has at a specific point in your code. There are a few functions in PHP that can help you do that. var_dump(), var_export() and print_r() are some of the functions you can use to do that. They do their work in slightly different manners:

function input output return value
var_dump "1.1" string(3) "1.1" no
var_dump 1.1 float(1.1) no
var_export "1.1" '1.1' optional
var_export 1.1 1.1 optional
print_r "1.1" 1.1 optional
print_r 1.1 1.1 optional

var_dump gives us the most information by providing us information about what type of variable it is, what the value and its type is. On the other hand, it is not as flexible as var_export or print_r if you want to store the output of the function somewhere, as it lacks a return value.

Debugging with a trace

debug_backtrace() is yet another function for debugging your code. It will get you a code trace of how you ended where you are. Here's an example of a simple "Hello Mark"-program:

<?php 

function hello(string $name) 
{
    $greeting = "Hello";
    if ($name != "Mark") {
        var_dump(debug_backtrace());
        return "";
    }
    return $greeting;
}

function sayHello() 
{
    echo hello("Bark");
}

sayHello();
Enter fullscreen mode Exit fullscreen mode

We get the following output when we run this code:

array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(54) "path\to\file\example.php"
    ["line"]=>
    int(15)
    ["function"]=>
    string(5) "hello"
    ["args"]=>
    array(1) {
      [0]=>
      string(4) "Bark"
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(54) "path\to\file\example.php"
    ["line"]=>
    int(18)
    ["function"]=>
    string(8) "sayHello"
    ["args"]=>
    array(0) {
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

The first function is added to the stack, with the latest function call added on top. We can see that the function hello was called with the argument "Bark" from line 15 in our code. That means that the function was called from the function sayHello, which in turn was called from line 18. We got this trace because we had added the debug_backtrace() to show how we ended up trying to greet someone not named "Mark".

These are somewhat static ways of debugging. There are more interactive ways to do it too, with phpdbg or xdebug, or maybe a library such as Symfony Profiler.

What about you?

Are you using any of these functions to debug your code? Perhaps you use echo statements instead? Is there a better way to debug the code, and how would you go about doing that? Comment below and let us know what you think ✍

Further Reading

Discussion (0)