Skip to Content
PHP Cookbook
book

PHP Cookbook

by Eric A. Mann
May 2023
Intermediate to advanced
431 pages
8h 55m
English
O'Reilly Media, Inc.
Content preview from PHP Cookbook

Chapter 13. Debugging and Testing

Despite developers’ best efforts, no code is ever perfect. You will inevitably introduce a bug that impacts the production behavior of your application or causes an end user distress when something doesn’t operate as expected.

Properly handling errors within your application is critical.1 However, not every error your application throws is expected—or even catchable. In these circumstances, you must understand how to properly debug your application—how to track down the offending line of code so it can be fixed.

Among the first steps any PHP engineer uses to debug their code is the echo statement. Without a formal debugger, it’s common to see development code littered with echo "Here!"; statements so the team can track where things might be broken.

The Laravel framework has made similar functionality popular and easily accessible while working on new projects by exposing a function called dd() (short for “dump and die”). This function is actually provided by the Symfony var-dumper module and works effectively in both PHP’s native command-line interface and when leveraging an interactive debugger. The function itself is defined as follows:

function dd(...$vars): void
{
    if (!in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && !headers_sent()) {
        header('HTTP/1.1 500 Internal Server Error');
    }

    foreach ($vars as $v) {
        VarDumper::dump($v);
    }

    exit(1);
}

The preceding function, when used in a Laravel application, will print the contents of any variable you ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Clean Code in PHP

Clean Code in PHP

Carsten Windler, Alexandre Daubois
JavaScript Cookbook, 3rd Edition

JavaScript Cookbook, 3rd Edition

Adam D. Scott, Matthew MacDonald, Shelley Powers

Publisher Resources

ISBN: 9781098121310Errata Page