Git Product home page Git Product logo

php-debug-functions's Introduction

Debug functions for PHP

CI codecov

A collection of simple debug functions which might be helpful in case a full-fledged debugger is not available.

Requirements

The following versions of PHP are supported:

  • PHP 7.2
  • PHP 7.3
  • PHP 7.4

Installation

composer install --dev fabacino/debug-functions

Usage

  • dbg: Print out debug value.
  • dbgr: Return debug value.
  • dbglog: Log debug value into file.
  • dbginit: Initialize debug settings.

The functions accept any value for debugging, as long as it can be passed as a variable.

Examples

Print values to standard output

dbg(123);
dbg('a string');
dbg([
    'token' => 'dp83kspo',
    'is_default' => true
]);
// Output
123
a string
Array
(
    [token] => dp83kspo
    [is_default] => true
)

When running in a non-CLI environment the ouput is wrapped around pre tags for better formatting.

Use dbginit or the flag USE_VARDUMP in case you prefer var_dump:

dbginit(['use_vardump' => true]);
dbg(123);
// or
dbg(123, Debug::USE_VARDUMP);
// which is the same as
dbg(123, 1);
// Output
int(123)
string(8) "a string"
array(2) {
  'token' => string(8) "dp83kspo"
  'is_default' => bool(true)
}

Use dbginit or the flag USE_HTMLENTITIES in case you want to encode HTML entities:

dbginit(['use_htmlentities' => true]);
dbg('<b>important</b>');
// or
dbg('<b>important</b>', Debug::USE_HTMLENTITIES);
// which is the same as
dbg('<b>important</b>', 2);
// Output
&lt;b&gt;important&lt;/b&gt;

Save debug output in variable

// dbgr is the same as dbg, except that the output is returned instead of printed.
$dbg = dbgr(123);

Log debug output to file

// dbglog is the same as dbg, except that the output is logged instead of printed.
dbginit(['log_file' => '/path/to/logfile']);
dbglog($this->doSomething());

Alternatively you can pass your own PSR-3 compatible logger:

$logger = new YourLogger();
...
dbginit(['logger' => $logger]);
dbglog($this->doSomething());

License

The MIT License (MIT). Please see License File for more information.

php-debug-functions's People

Contributors

fabacino avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.